先决条件: 带示例的正则表达式|Python
一种正则表达式(有时称为Rational表达式)是定义搜索模式的一系列字符, 主要用于与字符串进行模式匹配或字符串匹配(即类似”查找和替换”的操作)。正则表达式是一种将模式与字符序列匹配的通用方法。
模组正则表达式(RE)指定与其匹配的一组字符串(模式)。要了解RE类比, 元字符有用, 重要且将在模块功能中使用re.
一共有14个元字符, 将在功能中进行讨论:
\ Used to drop the special meaning of character
following it (discussed below)
[] Represent a character class
^ Matches the beginning
$ Matches the end
. Matches any character except newline
? Matches zero or one occurrence.
| Means OR (Matches with any of the characters
separated by it.
* Any number of occurrences (including 0 occurrences)
+ One ore more occurrences
{} Indicate number of occurrences of a preceding RE
to match.
() Enclose a group of REs
search()
search()方法要么返回None(如果模式不匹配), 要么返回re.MatchObject包含有关字符串匹配部分的信息。此方法在第一个匹配项后停止, 因此它最适合于测试正则表达式, 而不是提取数据。
例子:
# A Python program to demonstrate working of re.match().
import re
# Lets use a regular expression to match a date string
# in the form of Month name followed by day number
regex = r "([a-zA-Z]+) (\d+)"
match = re.search(regex, "I was born on June 24" )
if match ! = None :
# We reach here when the expression "([a-zA-Z]+) (\d+)"
# matches the date string.
# This will print [14, 21), since it matches at index 14
# and ends at 21.
print ( "Match at index % s, % s" % (match.start(), match.end()))
# We us group() method to get all the matches and
# captured groups. The groups contain the matched values.
# In particular:
# match.group(0) always returns the fully matched string
# match.group(1) match.group(2), ... return the capture
# groups in order from left to right in the input string
# match.group() is equivalent to match.group(0)
# So this will print "June 24"
print ( "Full match: % s" % (match.group( 0 )))
# So this will print "June"
print ( "Month: % s" % (match.group( 1 )))
# So this will print "24"
print ( "Day: % s" % (match.group( 2 )))
else :
print ( "The regex pattern does not match." )
输出如下:
Match at index 14, 21
Full match: June 24
Month: June
Day: 24
re.findall()
以字符串列表形式返回字符串中所有不重复的模式匹配项。从左到右扫描字符串, 并以找到的顺序返回匹配项。
例子:
# A Python program to demonstrate working of
# findall()
import re
# A sample text string where regular expression
# is searched.
string = """Hello my Number is 123456789 and
my friend's number is 987654321"""
# A sample regular expression to find digits.
regex = '\d+'
match = re.findall(regex, string)
print (match)
输出如下:
['123456789', '987654321']
首先, 你的面试准备可通过以下方式增强你的数据结构概念:Python DS课程。
评论前必须登录!
注册