当前位置:网站首页>Regular expressions: characters (2)
Regular expressions: characters (2)
2022-06-29 23:13:00 【Live up to your youth】
Regular expressions are made up of ordinary characters ( Such as character a To z) And the text mode composed of special characters . Special characters here are also called metacharacters , For different metacharacters , The meanings expressed in regular expressions are also different .
The components of a regular expression can be a single character 、 Character set 、 character in range 、 Choice between characters or any combination of all these components .
Ordinary character
Normal characters include all printable and nonprintable characters that are not explicitly specified as metacharacters . This includes all uppercase and lowercase letters 、 All figures 、 All punctuation and some other symbols .
Printable characters
| character | describe |
|---|---|
| a-z A-Z | Match lowercase a-z A-Z |
| 0-9 | Match the Numbers 0-9 |
| , . ; ’ ! | Match punctuation , . ; ’ ! etc. |
# Matches a single character in a string a
>>> pattern = r"a"
>>> string = "this is a line!"
>>> re.search(pattern, string)
<re.Match object; span=(8, 9), match='a'>
# Match a single number in a string 1
>>> pattern = r"1"
>>> string = "1+2=3"
>>> re.search(pattern, string)
<re.Match object; span=(0, 1), match='1'>
# Match the exclamation point in the string !
>>> pattern = r"!"
>>> string = "this is a line!"
>>> re.search(pattern, string)
<re.Match object; span=(14, 15), match='!'>
Nonprinting characters
| character | describe |
|---|---|
| \f | Match a page break . Equivalent to \x0c and \cL. |
| \n | Match a line break . Equivalent to \x0a and \cJ. |
| \r | Match a carriage return . Equivalent to \x0d and \cM. |
| \t | Match a tab . Equivalent to \x09 and \cI. |
| \v | Match a vertical tab . Equivalent to \x0b and \cK. |
# Match a line break
>>> pattern = r"\n"
>>>> string = "I like python.\nI like python too."
>>> re.search(pattern, string)
<re.Match object; span=(14, 15), match='\n'>
Special characters
Special characters have special meanings in regular expressions , If you need to match these special characters , You need to add a backslash to the front \. such as ,\\ Two backslashes match special characters \,\ * Match special character *.
| character | describe |
|---|---|
| \ | Mark the next character as a special character 、 Or an original character 、 Or a Backward reference 、 Or an octal escape character . |
| . | Match any character , Except for line breaks . |
| \b | Matches a word boundary , That is, the position between the word and the space . |
| \B | Match non word boundaries . |
| \cx | Match by x Control characters indicated . |
| \d | Matches a numeric character . Equivalent to [0-9]. |
| \D | Matches a non-numeric character . Equivalent to [^0-9]. |
| \s | Matches any whitespace characters , Including Spaces 、 tabs 、 Page breaks and so on . Equivalent to [ \f\n\r\t\v]. |
| \S | Matches any non-whitespace characters . Equivalent to [^ \f\n\r\t\v]. |
| \w | Match the letter 、 Numbers 、 Underline . Equivalent to ’[A-Za-z0-9_]'. |
| \W | Match nonletter 、 Numbers 、 Underline . Equivalent to ‘[^A-Za-z0-9_]’. |
| ^ | Matches the start of the input string . |
| $ | Matches the end of the input string . |
| a|b | matching a or b. |
| ( ) | Match the expression in brackets , It also means a group . |
| \1…\9 | Matching first n Grouped content . |
| [xyz] | Character set . Matches any of the contained characters . |
| [^xyz] | Negative character set . Match any character not included . |
| [a-z] | character in range . Matches any character in the specified range . |
| [^a-z] | Negative character range . Matches any character that is not in the specified range . |
| * | Match previous subexpression zero or more times . |
| + | Match previous subexpression one or more times . |
| ? | Match previous subexpression zero or once . |
| {n} | n Is a non negative integer . Matched definite n Time . |
| {n,} | n Is a non negative integer . Match at least n Time . |
| {n,m} | m and n All non negative integers , among n <= m. Least match n Times and at most m Time . |
# Match all the numbers
>>> string = "123abc456def"
>>> pattern = r"\d+"
>>> re.findall(pattern, string)
['123', '456']
# Match group ab A string that appears more than twice
>>> pattern = r"(ab){2,}"
>>> string = "abcabcababab"
>>> re.search(pattern, string)
<re.Match object; span=(6, 12), match='ababab'>
# Match in set a, b, c String that appears more than once
>>> pattern = r"[abc]{1,}"
>>> string = "bcdfghijkl"
>>> re.search(pattern, string)
<re.Match object; span=(0, 2), match='bc'>
>>> pattern = r"[abc]+"
# use + Instead of {1,}, The result is equivalent
>>> re.search(pattern, string)
>>> string = "bcdfghijkl"
<re.Match object; span=(0, 2), match='bc'>
Let's take a look at an example of mailbox matching :
# Mailbox number matching
>>> pattern = r"\b[\w.%+-][email protected][\w.-]+\.[a-zA-Z]{2,6}\b"
>>> string = "[email protected]"
>>> re.search(pattern, string)
<re.Match object; span=(0, 14), match='[email protected]'>

边栏推荐
- 正则表达式:字符(2)
- Mysql database: storage engine
- laravel 关联模型 多态关系
- Realizing deep learning framework from zero -- RNN from theory to practice [practice]
- Is it safe to open an account on the flush? Where to apply for opening an account
- How ZABBIX 5.0 adds esxi6.7 to monitoring
- Problem solving metauniverse, multi communication scheme in online games
- Detailed description of gaussdb (DWS) complex and diverse resource load management methods
- MySQL backup database Linux
- 基金的估值,费用,会计核算
猜你喜欢

CE第二次作业

Go zero micro Service Practice Series (VII. How to optimize such a high demand)

Wechat applet: picture seconds plus watermark generation

nrm详解

Hezhou air32f103cbt6 development board hands-on Report

redis客户端

wirehark数据分析与取证infiltration.pacapng

Free PDF to word software sharing, these software must know!

Can you be a coder if you don't learn English well? Stop worrying and learn it first

微博系统中”微博评论“的高性能高可用计算架构
随机推荐
Deep parsing of kubernetes controller runtime
分布式消息中间件设计
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
5-2Web应用程序漏洞扫描
Kr-gcn: an interpretable recommendation system based on knowledge aware reasoning
AI场景存储优化:云知声超算平台基于 JuiceFS 的存储实践
自己收藏的一些网址
leetcode 416. Partition Equal Subset Sum 分割等和子集(中等)
正则表达式:字符(2)
Wireshark data analysis and forensics information pacapng
Wechat applet: (update) cloud development wechat group contacts
疫情下我离职一年,收入增长了10倍
laravel 关联模型 多态关系
Talk about auto in MySQL in detail_ What is the function of increment
优雅的改造短信业务模块,策略模式走起!
Steady! The best posture for thousands of microservices to access Zadig (helm chart)
还天天熬夜加班做报表?其实你根本不懂如何高效做报表
VS无法定位程序输入点于动态链接库
mysql备份数据库linux
[learn FPGA programming from scratch -51]: high level chapter - FPGA development based on IP core - what is FPGA IP core (soft core, fixed core, hard core) and learning methods