【Data Preprocessing】Python使用正则表达式入门速查-Regular Expression Quick View
regular expression
This article is intended to introduce the RE package in Python.
The main funciton of RE module is
- sub:Replace matched items.
- match:Match from the beginning.
- search:Search the entire string.
- findall:Return a list of all matches.
- finditer:Return a list of all matches.
- split:Split a string based on a regex.
- compile:Complie a regex for effciency.
- escape:Escape special characters.
sub
Replace parts of the string that match the pattern wiht repl.
re.sub(pattern, repl, string, count=0, flags=0)
Demo
import re
result = re.sub(r'\d+', 'X', 'There are 6 straberries and 12 grapes')
print("Result:", result)
output:
Result: There are X straberries and X grapes.
match
Match the regular expression pattern from the beginning of the string.
re.match(pattern, string, flags=0)
Demo
import re
match = re.match(r'^Hello', 'Hello, world!')
if match:
print("Match found:", match.group())
else:
print("No match")
output:
Match found: Hello
search
Search the entire string for the first match of the regular expression.
re.search(pattern, string, flags=0)
Demo
import re
search = re.search(r'world', 'Hello, world!')
if search:
print("Match found:", search.group())
else:
print("No match")
output:
Match found: world
findall.
Return all non-overlapping matches of the pattern in the string as a list of strings.
matches = re.findall(pattern, sting, flags=0)
Demo
matches = re.findall(r'\d+', 'There are 6 strawberries and 12 grapes.')
print("Matches:", matches)
output:
Matches: ['6', '12']
finditer
Retirm an iterator yielding Match objects for all non-overlapping matches of the pattern in the string.
re.finditer(pattern, string, flags=0)
Demo
for match in re.finditer(r'\d+', 'There are 6 strawberries and 12 grapes.'):
print("Match:", match.group(), "at position:", match.span())
output:
Match: 6 at position: (10, 11)
Match: 12 at position: (29, 31)
split
Split the string by occurrences of the pattern.
re.split(pattern, string, maxsplit=0, flags=0)
Demo
result = re.split(r'\s+', 'Hello world this is Python written by Jellyfish')
print("Result:", result)
output:
Result: ['Hello', 'world', 'this', 'is', 'Python', 'written', 'by', 'Jellyfish']
compile
Compile a regular expression pattern into a Pattern object for more efficient use.
re.compile(pattern, flags=0)
Demo
pattern = re.compile(r'\d+')
matches = pattern.findall('There are 6 strawberries and 12 grapes.')
print("Matches:", matches)
output:
Matches: ['6', '12']
escape
Escape special characters in the string.
re.escape(string)
Demo
escaped = re.escape('Hello [world]!')
print("Escaped:", escaped)
output:
Escaped: Hello\ \[world\]!
【Data Preprocessing】Python使用正则表达式入门速查-Regular Expression Quick View的更多相关文章
- [转载]正则表达式参考文档 - Regular Expression Syntax Reference.
正则表达式参考文档 - Regular Expression Syntax Reference. [原创文章,转载请保留或注明出处:http://www.regexlab.com/zh/regref. ...
- Spring Data JPA简单查询接口方法速查
下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.(1)先按照功能进行分类 ...
- GNU Emacs命令速查表
GNU Emacs命令速查表 第一章 Emacs的基本概念 表1-1:Emacs编辑器的主模式 模式 功能 基本模式(fundamental mode) 默认模式,无特殊行为 文本模式(text m ...
- python study - 正则表达式
第 7 章 正则表达式 7.1. 概览 7.2. 个案研究:街道地址 7.3. 个案研究:罗马字母 7.3.1. 校验千位数 7.3.2. 校验百位数 7.4. 使用 {n,m} 语法 7.4.1. ...
- 正则表达式入门+实战(c#实现)
如果有人和你说,如果不将字符串转换为数字,你要如何判断字符串是否由全数字组成?把字符串拆成char数组,然后放入一个循环,来判断每个char是否为数字?那你要如何判断手机号是否合法?IP是否合法呢?把 ...
- python轻量级orm框架 peewee常用功能速查
peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM.它有很少的(但富有表现力的)概念,使它易于学习和直观的使用. 常见orm数据库框架 Django ORM peewee ...
- Python 正则表达式入门(中级篇)
Python 正则表达式入门(中级篇) 初级篇链接:http://www.cnblogs.com/chuxiuhong/p/5885073.html 上一篇我们说在这一篇里,我们会介绍子表达式,向前向 ...
- Python 正则表达式入门(初级篇)
Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. 转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达 ...
- 转载 Python 正则表达式入门(初级篇)
Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写.转载请写明出处 引子 首先说 正则表达式是什么? 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式 ...
- python正则表达式入门篇
文章来源于:https://www.cnblogs.com/chuxiuhong/p/5885073.html Python 正则表达式入门(初级篇) 本文主要为没有使用正则表达式经验的新手入门所写. ...
随机推荐
- WPF关于 附加属性绑定异常 ,导致页面加载缓慢
最近项目在启动的时候,经常出现Xmal的类型转换的异常 :引发的异常:"MS.Internal.Xaml.Parser.GenericTypeNameParser.TypeNamePars ...
- 简单介绍List和数组转List集合
目录 综述 如何创建List 六种数组转List的方法 for循环遍历 Arrays.asList() new ArrayList<>(Arrays.asList(array)) Coll ...
- Go与C/C++ 互相调用
A. Go调用C 1.Go调用C:在go文件里调C(以下代码中除了开头的注释之外,其他注释不可删除) /* * go 和 C 互调用程序 */ package main /* int Add( int ...
- Kafka入门实战教程(3).NET Core操作Kafka
1 可用的Kafka .NET客户端 作为一个.NET Developer,自然想要在.NET项目中集成Kafka实现发布订阅功能.那么,目前可用的Kafka客户端有哪些呢? 目前.NET圈子主流使用 ...
- 分时间段(年份或月份)统计,没有数字补0 Java(替代 MYSQL) 做法
需求如下~ 输入年份,表格第一行 1-12 月 输入年份和月份 表格第一行 1--31 具体天数 表格第二行就是统计数量,没有补0. 看完首先想到MYSQL查询出连续一段时间和数量,没有 就为0. ...
- 21verilog函数
Verilog函数详解 目录 1. 函数简介 2. 函数基本语法 3. 函数特性与限制 4. 函数参数与返回值 5. 常数函数 6. automatic函数 7. 函数应用场景 8. 最佳实践与注意事 ...
- java--jdbc加强
事务 事务使指一组最小逻辑操作单元,里面有多个操作组成. 组成事务的每一部分必须要同时提交成功,如果有一个操作失败,整个操作就回滚. 事务ACID特性 原子性(Atomicity) 原子性是指事务 ...
- sql server的 安装说明与基本工具概念
shared memory(共享内存) 代表连接本机的数据库:Named Pipes(命名管道)针对局域网内的数据库链接:tcp/ip 针对互联网内的数据库链接 ------数据库的操作: 1.联机事 ...
- java基础--List
List基本属性和方法移步官方文档: List (Java Platform SE 8 ) 1.处理最简单的List<String>: (1)并集.交集.差集 并集: 如果只用List.a ...
- centos+apache安装ssl
默认apache是没有安装SSL模块的,如果网站想以https方式访问,则需要安装并配置ssl模块 第一步:下载ssl证书不做介绍,下载完成后,证书一般包括3个文件_public.crt文件是证书文件 ...