当前位置:网站首页>Improve the readability of your regular expressions a hundred times
Improve the readability of your regular expressions a hundred times
2022-07-29 04:48:00 【The way of Python data】
author :kingname
source : Unheard of Code
Regular expressions , Powerful is powerful , But it's written like an emoticon . Write your own expression , Come and see in a month , I don't remember what it means . Like this one down here :
pattern = r"((?:\(\s*)?[A-Z]*H\d+[a-z]*(?:\s*\+\s*[A-Z]*H\d+[a-z]*)*(?:\s*[\):+])?)(.*?)(?=(?:\(\s*)?[A-Z]*H\d+[a-z]*(?:\s*\+\s*[A-Z]*H\d+[a-z]*)*(?:\s*[\):+])?(?![^\w\s])|$)"Is there any way to improve the readability of regular expressions ? We know , One way to improve code readability is to write comments , So can regular expressions write comments ?
For example, for the following sentence :
msg = ' My name is Qingnan , My password is :123kingname456, Please keep it confidential .' I want to extract the password 123kingname456, Then my regular expression may be like this :
pattern = ':(.*?),'Can I write it like this :
pattern = '''
: # Start sign
(.*?) # Any character starting from the next character of the start flag
, # Stop when you encounter an English comma
'''It's much clearer to write in this way , The function of each part is clear .
But obviously, nothing can be extracted directly , As shown in the figure below :

But I'm shopping today Python Regular expression documents [1] When , Found a good thing :

Use it , You can make your regular expressions have comments , As shown in the figure below :

re.VERBOSE It can also be abbreviated as re.X, As shown in the figure below :

Complex regular expressions at the beginning of this article , After using comments , It will become more readable :
pattern = r"""
( # code (capture)
# BEGIN multicode
(?: \( \s* )? # maybe open paren and maybe space
# code
[A-Z]*H # prefix
\d+ # digits
[a-z]* # suffix
(?: # maybe followed by other codes,
\s* \+ \s* # ... plus-separated
# code
[A-Z]*H # prefix
\d+ # digits
[a-z]* # suffix
)*
(?: \s* [\):+] )? # maybe space and maybe close paren or colon or plus
# END multicode
)
( .*? ) # message (capture): everything ...
(?= # ... up to (but excluding) ...
# ... the next code
# BEGIN multicode
(?: \( \s* )? # maybe open paren and maybe space
# code
[A-Z]*H # prefix
\d+ # digits
[a-z]* # suffix
(?: # maybe followed by other codes,
\s* \+ \s* # ... plus-separated
# code
[A-Z]*H # prefix
\d+ # digits
[a-z]* # suffix
)*
(?: \s* [\):+] )? # maybe space and maybe close paren or colon or plus
# END multicode
# (but not when followed by punctuation)
(?! [^\w\s] )
# ... or the end
| $
)
"""Reference material
[1]
Regular expression documents : https://docs.python.org/3/library/re.html#re.VERBOSE
-------- End --------

Selected content
The illustration Pandas- Image & Text 01- Data structure introduction
The illustration Pandas- Image & Text 02- Create data objects
The illustration Pandas- Image & Text 03- Read and store Excel file
The illustration Pandas- Image & Text 04- Common data access
The illustration Pandas- Image & Text 05- Common data operations
The illustration Pandas- Image & Text 06- Common mathematical calculations
The illustration Pandas- Image & Text 08- Common data filtering


边栏推荐
- OpenCV环境搭建
- File operation (Advanced C language)
- STL source code analysis (Hou Jie) notes - STL overview
- ios面试准备 - 网络篇
- GCC Basics
- 1 句代码,搞定 ASP.NET Core 绑定多个源到同一个类
- Recyclerview switches the focus up and down through the dpad key. When switching to the control outside the interface, the focus will jump left and right
- Whether the modification of import and export values by ES6 and commonjs affects the original module
- 盒子水平垂直居中布局(总结)
- Webrtc realizes simple audio and video call function
猜你喜欢

Reveal installation configuration debugging
![Academic | [latex] super detailed texlive2022+tex studio download installation configuration](/img/4d/f8c60c0fbbd98c4da198cfac7989fa.png)
Academic | [latex] super detailed texlive2022+tex studio download installation configuration

Flink+Iceberg环境搭建及生产问题处理

Pycharm reports an error when connecting to the virtual machine database

IOS interview preparation - IOS

Unity基础(3)—— unity中的各种坐标系

Use more flexible and convenient Rogowski coil

DASCTF2022.07赋能赛

UE plays video in scene or UMG

Use of construction methods
随机推荐
Corresponding order of 18 and 25coco data of openpose and joint points
iOS面试准备 - ios篇
Classes and objects (III)
UE plays video in scene or UMG
Google browser opens the web page and out of memory appears
Oracle insert data
Recommendation system of online education
DASCTF2022.07赋能赛
JVM (heap and stack) memory allocation
Post export data, return
Software test interview questions (4)
SSM integration, addition, deletion, modification and query
How to build a mobile studio network?
如何避免示波器电流探头损坏
Classes and objects (II)
ssm整合增删改查
[C language] PTA 7-47 binary leading zero
MySQL - clustered index and secondary index
Configure st-gcn environment record [Google lab]
Implementation of flutter gesture monitoring and Sketchpad