当前位置:网站首页>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’


边栏推荐
- Load other related resources or configurations (promise application of the applet) before loading the homepage of the applet
- 什么是乐观锁,什么是悲观锁
- [IVI] 15.1.2 system stability optimization (lmkd Ⅱ) psi pressure stall information
- Fast construction of neural network
- Unity WebGL发布无法运行问题
- [QNX Hypervisor 2.2用户手册]12.2 术语(二)
- 反爬之验证码识别登录 (OCR字符识别)
- 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
- 深度学习论文阅读目标检测篇(七)中文版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
猜你喜欢

报错ImportError: numpy.core.multiarray failed to import

编辑类型信息

Drawing with MATLAB (2) -- color ring

Selenium builds cookies pool to bypass authentication and anti crawl login

Phpcms V9 mobile phone access computer station one-to-one jump to the corresponding mobile phone station page plug-in

phpcms v9后台文章列表增加一键推送到百度功能

Srv6---is-is extension

基于SSM的毕业论文管理系统

【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(一)

百度小程序富文本解析工具bdParse
随机推荐
1.21 study gradient descent and normal equation
隐藏式列表菜单以及窗口转换在Selenium 中的应用
深度学习论文阅读目标检测篇(七)中文版:YOLOv4《Optimal Speed and Accuracy of Object Detection》
ImportError: ERROR: recursion is detected during loading of “cv2“ binary extensions. Check OpenCV in
Basic concept and advanced level of behavior tree
Code de mise en œuvre de l'intercepteur et du filtre
Phpcms V9 remove the phpsso module
反爬之验证码识别登录 (OCR字符识别)
【程序的编译和预处理】
How to use the least money to quickly open the Taobao traffic portal?
commonJS和ES6模块化的区别
如何编译构建
External sorting and heap size knowledge
Introduction to common classes on the runtime side
Drawing with MATLAB (1)
教程1:Hello Behaviac
Autoregressive model of Lantern Festival
Phpcms applet interface new universal interface get_ diy. php
[cloud primordial | kubernetes chapter] go deep into the foundation of all things - container (V)
[qnx hypervisor 2.2 user manual]12.1 terminology (I)