This article introduces Python’s support for regular expressions, including the basics of regular expressions and a complete introduction and usage examples of the Python regular expression standard library. The content of this article does not include how to write efficient regular expressions or how to optimize regular expressions. Please check other tutorials for these topics.
Note: This article is based on Python2.4; if you see vocabulary you don’t understand, please remember Baidu, Google or Wiki, whatever.
Respect the author's work, please indicate the author and original address when reprinting>.html
Regular expressions are not part of Python. Regular expressions are a powerful tool for processing strings. They have their own unique syntax and an independent processing engine. They may not be as efficient as str's own method, but they are very powerful. Thanks to this, the syntax of regular expressions is the same in languages that provide regular expressions. The only difference is that different programming language implementations support different numbers of syntaxes; but don’t worry, unsupported syntaxes are usually It is a part that is not commonly used. If you have already used regular expressions in other languages, you only need to take a quick look to get started.
The following figure shows the matching process using regular expressions:
The general matching process of regular expressions is: take out the expression and the characters in the text in turn Comparison, if every character can be matched, the match is successful; if there is an unsuccessful match, the match fails. If there are quantifiers or boundaries in the expression, the process is slightly different, but it is easy to understand. You can understand it by looking at the example in the picture below and using it a few times yourself.
The following figure lists the regular expression metacharacters and syntax supported by Python:
Regular expressions are commonly used to find matching strings in text. Quantifiers in Python are greedy by default (or non-greedy by default in a few languages), always trying to match as many characters as possible; non-greedy, on the contrary, always try to match as few characters as possible. For example: if the regular expression "ab*" is used to find "abbbc", "abbb" will be found. And if you use the non-greedy quantifier "ab*?", you will find "a".
Like most programming languages, "" is used as an escape character in regular expressions, which may cause backslash troubles. If you need to match the character "" in the text, then the regular expression expressed in the programming language will need 4 backslashes "\\": the first two and the last two are used to escape into in the programming language Backslash, converted into two backslashes and then escaped into one backslash in the regular expression. The native strings in Python solve this problem very well. The regular expression in this example can be represented by r"\". Likewise, "\d" matching a number can be written as r"d". With native strings, you no longer have to worry about missing backslashes, and the expressions you write are more intuitive.
Regular expressions provide some available matching patterns, such as ignoring case, multi-line matching, etc. This part will be found in the factory method re.compile of the Pattern class (pattern[, flags]) are introduced together.
Python provides support for regular expressions through the re module. The general steps for using re are to first compile the string form of the regular expression into a Pattern instance, then use the Pattern instance to process the text and obtain the matching result (a Match instance), and finally use the Match instance to obtain information and perform other operations.
# encoding: UTF-8 import re # 将正则表达式编译成Pattern对象 pattern = re.compile(r'hello') # 使用Pattern匹配文本,获得匹配结果,无法匹配时将返回None match = pattern.match('hello world!') if match: # 使用Match获得分组信息 print match.group() ### 输出 ### # hello
re.compile(strPattern[, flag]):
This method is the factory method of the Pattern class, used to compile regular expressions in the form of strings into Pattern objects. The second parameter flag is the matching mode. The value can use the bitwise OR operator '|' to indicate that it is effective at the same time, such as re.I | re.M. In addition, you can also specify the pattern in the regex string. For example, re.compile('pattern', re.I | re.M) is equivalent to re.compile('(?im)pattern').
Optional values are:
a = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X) b = re.compile(r"\d+\.\d*")
re提供了众多模块方法用于完成正则表达式的功能。这些方法可以使用Pattern实例的相应方法替代,唯一的好处是少写一行re.compile()代码,但同时也无法复用编译后的Pattern对象。这些方法将在Pattern类的实例方法部分一起介绍。如上面这个例子可以简写为:
m = re.match(r'hello', 'hello world!') print m.group()
re模块还提供了一个方法escape(string),用于将string中的正则表达式元字符如*/+/?等之前加上转义符再返回,在需要大量匹配元字符时有那么一点用。
Match对象是一次匹配的结果,包含了很多关于此次匹配的信息,可以使用Match提供的可读属性或方法来获取这些信息。
属性:
方法:
import re m = re.match(r'(\w+) (\w+)(?P<sign>.*)', 'hello world!') print "m.string:", m.string print "m.re:", m.re print "m.pos:", m.pos print "m.endpos:", m.endpos print "m.lastindex:", m.lastindex print "m.lastgroup:", m.lastgroup print "m.group(1,2):", m.group(1, 2) print "m.groups():", m.groups() print "m.groupdict():", m.groupdict() print "m.start(2):", m.start(2) print "m.end(2):", m.end(2) print "m.span(2):", m.span(2) print r"m.expand(r'\2 \1\3'):", m.expand(r'\2 \1\3') ### output ### # m.string: hello world! # m.re: <_sre.SRE_Pattern object at 0x016E1A38> # m.pos: 0 # m.endpos: 12 # m.lastindex: 3 # m.lastgroup: sign # m.group(1,2): ('hello', 'world') # m.groups(): ('hello', 'world', '!') # m.groupdict(): {'sign': '!'} # m.start(2): 6 # m.end(2): 11 # m.span(2): (6, 11) # m.expand(r'\2 \1\3'): world hello!
Pattern对象是一个编译好的正则表达式,通过Pattern提供的一系列方法可以对文本进行匹配查找。
Pattern不能直接实例化,必须使用re.compile()进行构造。
Pattern提供了几个可读属性用于获取表达式的相关信息:
import re p = re.compile(r'(\w+) (\w+)(?P<sign>.*)', re.DOTALL) print "p.pattern:", p.pattern print "p.flags:", p.flags print "p.groups:", p.groups print "p.groupindex:", p.groupindex ### output ### # p.pattern: (\w+) (\w+)(?P<sign>.*) # p.flags: 16 # p.groups: 3 # p.groupindex: {'sign': 3}
实例方法[ | re模块方法]:
# encoding: UTF-8 import re # 将正则表达式编译成Pattern对象 pattern = re.compile(r'world') # 使用search()查找匹配的子串,不存在能匹配的子串时将返回None # 这个例子中使用match()无法成功匹配 match = pattern.search('hello world!') if match: # 使用Match获得分组信息 print match.group() ### 输出 ### # world
import re p = re.compile(r'\d+') print p.split('one1two2three3four4') ### output ### # ['one', 'two', 'three', 'four', '']
import re p = re.compile(r'\d+') print p.findall('one1two2three3four4') ### output ### # ['1', '2', '3', '4']
import re p = re.compile(r'\d+') for m in p.finditer('one1two2three3four4'): print m.group(), ### output ### # 1 2 3 4
import re p = re.compile(r'(\w+) (\w+)') s = 'i say, hello world!' print p.sub(r'\2 \1', s) def func(m): return m.group(1).title() + ' ' + m.group(2).title() print p.sub(func, s) ### output ### # say i, world hello! # I Say, Hello World!
import re p = re.compile(r'(\w+) (\w+)') s = 'i say, hello world!' print p.subn(r'\2 \1', s) def func(m): return m.group(1).title() + ' ' + m.group(2).title() print p.subn(func, s) ### output ### # ('say i, world hello!', 2) # ('I Say, Hello World!', 2)
以上就是Python对于正则表达式的支持。熟练掌握正则表达式是每一个程序员必须具备的技能,这年头没有不与字符串打交道的程序了。笔者也处于初级阶段,与君共勉,^_^
另外,图中的特殊构造部分没有举出例子,用到这些的正则表达式是具有一定难度的。有兴趣可以思考一下,如何匹配不是以abc开头的单词,^_^
全文结束