当前位置:网站首页>Regular expression
Regular expression
2022-06-26 09:08:00 【Feng Dashao】
Regular expressions themselves are small 、 Highly specialized programming language , And in the python in , Through embedded Integration re modular , Can be called directly to achieve regular matching , Retrieve and replace those that match a pattern ( The rules ) Text, etc .
The following describes common functions and their parameters , Modifier , And the usage of various pattern rules .





compile(pattern, flags=0)
For compiling regular expressions , Generate a regular expression ( Pattern ) Object supply match() and search() And so on .
Match at least one number from the beginning of the string , return None
From specified Index 9,13 Start matching at the string position of , return None
From specified Index 10,13 Start matching at the string position of , return 12, The match is successful 
return Index Starting position 
return Index End position 
return Index Range 
Another one compile Function example : Add modifier re.I and group() Method directly outputs the matching string 
group(1) and group(2) The output results are grouping 1, 2 Result 
groups() Method , Direct output contains grouping 1 And grouping 2 Of tuple
amount to result.groups() = (result.group(1), result.group(2)…)
therefore group(1) & group(2) It can also be done through tuple Method of value taking , Direct output 

match(pattern, string, flags=0)
from string Start at the beginning of the string Match target object pattern, If the match is successful , Return match object ; If no match is found , Then for “None”


fullmatch(pattern, string, flags=0)
Try the regular expression pattern Apply to all strings string, If 100% The match is successful , Return match object ; If no match is found , Then for “None”


search(pattern, string, flags=0)
Scan string string To find a regular expression pattern Matches for , Return match object ; If no match is found , Then for “None”

split(pattern, string, maxsplit=0, flags=0)
By regular expression pattern The rules of Division maxsplit Substring , And return to the list
With “b* " by pattern object , Because the string has no letters b, Add *b pattern amount to ’ '( Blank content ) No matching letters b, Split each character of the string

hold * Get rid of , It is equivalent to not matching the corresponding character , No segmentation , Finally, output the list of original strings 
add to maxsplit by 1 when , Output contain 2 A string A list of 
With ’ \W+’ by pattern, matching 1 One or more non alphanumeric characters and underscores , Split the string into 4 A list of strings , The last character is null 
With (’ \W+’ ) by pattern, String in the output list , The symbol contained in the original string is an independent character in the list 
The same to (’ \W+’ ) by pattern, Add a space at the beginning of the original string
Number in the output list 2 A string , For a space

With ’ \W+’ by pattern, add to maxsplit by 1 when , Output contain 2 A string A list of 
With ‘r\s+’ by pattern, matching 1 One or more arbitrary white space characters , Split into 4 A list of characters
amount to Direct string splitting method : str.split()

findall(pattern, string, flags=0)
In string string Match all regular expressions in pattern The object of , And pass these objects through the list list Form return of
With r’\W+’ by pattern, matching 1 One or more non alphanumeric characters and underscores , Output, respectively, 2 A list ,1 Yes Space and ! A list of independent characters ; The other is Only Space A list of , By adding Index Range

sub(pattern, repl, string, count=0, flags=0)
Use regular expressions pattern To match the original string string, And replace the matched content with repl, It can be used count Specify the number of replacements
repl : Replaced string , It can also be a function
string : The original string to be found and replaced
count : The maximum number of substitutions after pattern matching , Default 0 Means to replace all matches
With r’\D’ by pattern, Match any non number , Put the number of ’-‘ Get rid of

With r’#.*$’ by pattern, Match any character ( except \n Outside ), Put the back of the phone Remove comments from

subn(pattern, repl, string, count=0, flags=0)
Use regular expressions pattern To match the original string string, And replace the matched content with repl And return the quantity replaced
With r’\D’ by pattern, Match any non number , Put the number of ’-‘ Get rid of , The final result is a tuple, Contains the matching results And the number of matches

finditer(pattern, string, flags=0)
In string string Match all regular expressions in pattern The object of , And return these objects in the form of iterators
With r’\d+’ by pattern, matching 1 One or more numbers , hold 12, 32, 43 and 3 Save to iterator , Through the loop , Output 4 Group numbers

escape(pattern)
Escape a special character in a string , That is to add... Before each special character \

re.I (re.IGNORECASE)
Make match match case insensitive

re.M (re.MULTILINE)
Multi-line matching , Multi line mode , When there is a line break in a string \n, Line breaks are not supported in default mode , such as : The beginning of the line and End of line , In multiline mode, matching line start is supported

re.S (re.Dotall)
DOT Express .,ALL Express all , All in all . Match all , Include line breaks \n
In default match mode . There is no match for newline \n, Instead, match strings separately ; And in the re.DOTALL In mode , A newline \n Match with string to .
re.X (re.VERBOSE)
Detailed mode , You can annotate regular expressions

{n}
matching n Time
With r’\d{8}’ by pattern, Before matching 8 Digit number 
{n,}
Match at least n Time
With r’\d{10,}’ by pattern, Strings share 11 position , The matching result is 11 digit , Conform to the rules 
When it comes to {12,} , because n < len(str) , So there's an error 
{n,m}
Match at least n Times and no more than m Time (n<=m)
With r’\d{9,10}’ by pattern, The result is The complete string is 10 Digit number 
If replaced {10,9 } because n < m, Not in line with the rules , Will report a mistake 
If { 9,13}, although 13 Beyond the len(str) , But it can also be matched successfully , It is equivalent to that the slice is out of range , No errors reported .
\b
Matches a word boundary , for example , ‘er\b’ Can match ‘worker’ Medium ‘er’, But can't match ‘every’ Medium ‘er’


\B
Match non word boundaries .‘er\B’ Can match ‘every’ Medium ‘er’, But can't match ‘worker’ Medium ‘er’


边栏推荐
- Yolov5进阶之四训练自己的数据集
- Phpcms applet interface new universal interface get_ diy. php
- Data warehouse (3) star model and dimension modeling of data warehouse modeling
- 框架跳转导致定位失败的解决方法
- Practice is the fastest way to become a network engineer
- Yolov5进阶之二安装labelImg
- 1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
- Yolov5进阶之三训练环境
- Uniapp uses uparse to parse the content of the background rich text editor and modify the uparse style
- The solution of positioning failure caused by framework jump
猜你喜欢

In depth study paper reading target detection (VII) Chinese version: yolov4 optimal speed and accuracy of object detection

Yolov5进阶之四训练自己的数据集

百度小程序富文本解析工具bdParse

phpcms v9手机访问电脑站一对一跳转对应手机站页面插件

Games104 Lecture 12 游戏引擎中的粒子和声效系统

phpcms小程序插件4.0版正式上线

Matlab drawing checkerboard (camera calibration)

微信小程序如何转换成百度小程序

Slider verification - personal test (JD)

isinstance()函数用法
随机推荐
Introduction to common classes on the runtime side
[QNX Hypervisor 2.2用户手册]12.2 术语(二)
【开源】使用PhenoCV-WeedCam进行更智能、更精确的杂草管理
9. code generation
框架跳转导致定位失败的解决方法
1.21 study logistic regression and regularization
读书笔记:SQL 查询中的SQL*Plus 替换变量(DEFINE变量)和参数
Section IV HQL execution process
力扣399【除法求值】【并查集】
1.17 daily improvement of winter vacation learning (frequency school and Bayesian school) and maximum likelihood estimation
SRv6----IS-IS扩展
isinstance()函数用法
自动化测试中,三种常用的等待方式,强制式(sleep) 、 隐式 ( implicitly_wait ) 、显式(expected_conditions)
ThreadLocal
Phpcms applet interface new universal interface get_ diy. php
拦截器与过滤器的实现代码
Unity 接入图灵机器人
唯品会工作实践 : Json的deserialization应用
How to set the shelves and windows, and what to pay attention to in the optimization process
百度小程序富文本解析工具bdParse