^:字符串开始位置与匹配规则符合就匹配,不然不匹配python
匹配字符串开头。在多行模式中匹配每一行的开头(Python3+已经失效,配合compile使用)正则表达式
import re s = 'abc123456abc' res = re.findall('^ab',s) print(res)
['ab']django
$:字符串结束位置与匹配规则符合就匹配,不然不匹配函数
匹配字符串末尾,在多行模式中匹配每一行的末尾spa
import re s= 'abc12ab56bc' res = re.findall('bc$',s) print(res)
['bc']code
import re s= 'abc12ab56bc' res = re.findall('[abc]',s) print(res)
['a', 'b', 'c', 'a', 'b', 'b', 'c']对象
import re s= 'abc12ab56bc' res = re.findall('[^ab]',s) print(res)
['c', '1', '2', '5', '6', 'c']get
(.:) 任意字符(除了\n)it
字符后有几个点就取字符+几个点的长度
import re s= 'abc12ab56bc' res = re.findall('ab.',s) print(res)
['abc', 'ab5']
import re s= 'abc12ab56bc' res = re.findall('ab...',s) print(res)
['abc1', 'ab56']
import re s= 'abc12ab56bc' res = re.findall('ab...',s) print(res)
['abc12', 'ab56b']
*: 前面的字符0-无穷个
import re s= 'ab2aabbacaaa' res = re.findall('a*',s) print(res)
['a', '', '', 'aa', '', '', 'a', '', 'aaa', '']
+: 前面的字符1-无穷个
import re s= 'ab2aabbacaaa' res = re.findall('a+',s) print(res)
['a', 'aa', 'a', 'aaa']
?: 前面的字符0-1个
import re s= 'ab2aabbacaaa' res = re.findall('a?',s) print(res)
['a', '', '', 'a', 'a', '', '', 'a', '', 'a', 'a', 'a', '']
{m}: 前面的字符m个
import re s= 'ab2aabbacaaa' res = re.findall('a{2}',s) print(res)
['aa', 'aa']
{n,m}: 前面的字符n-m个
import re s= 'ab2aabbacaaa' res = re.findall('a{1,3}',s) print(res)
['a', 'aa', 'a', 'aaa']
预约义字符是在字符集和组里都是有用的
反斜杠后边跟普通字符实现特殊功能
import re 1.\d: 数字 s = 's 1 s+\n=$\t2_s 3' print(re.findall('\d', s)) # ['1', '2', '3'] 2.\D: 非数字 print(re.findall('\D', s)) # ['s', ' ', ' ', ' ', ' ', ' ', 's', '+', '\n', '=', '$', '\t', '_', 's', ' ', ' '] 3. \w: 数字/字母/下划线 print(re.findall('\w', s)) # ['s', '1', 's', '2', '_', 's', '3'] 4.\W: 非数字/字母/下划线 print(re.findall('\W', s)) [' ', ' ', ' ', ' ', ' ', '+', '\n', '=', '$', '\t', ' ', ' '] 5.\s: 空格/\t/\n print(re.findall('\s', s)) # [' ', ' ', ' ', ' ', ' ', '\n', '\t', ' ', ' '] 6. \S: 非空格/\t/\n print(re.findall('\S', s)) # ['s', '1', 's', '+', '=', '$', '2', '_', 's', '3'] 7.\: 取消意义 s = 'aba\d' print(re.findall(r'a\\d', s)) # ['a\\d'] 8. .*: 贪婪模式(最大化),找到继续找,让结果最大化 s = 'abbbcabc' print(re.findall('a.*c', s)) # ['abbbcabc'] 9..*?: 非贪婪模式(最小化),找到就立刻中止 s = 'abbbcabc' print(re.findall('a.*?c', s)) ['abbbc', 'abc'] 10. (): 只要括号内的 s = 'abacad' print(re.findall('a(.)', s)) # 除了a都要,一次只匹配一个字符 # ['b', 'c', 'd'] 11. A|B: A和B都要 s = 'abacad' print(re.findall('a|b', s)) #['a', 'b', 'a', 'a']
s = '123abc123\ndef456' print(re.findall('\d+', s)) # 匹配全部我的数字
['123', '123', '456']
res = re.match('\d+', s) print(res) print(res.group())
<_sre.SRE_Match object; span=(0, 3), match='123'>
123
res = re.search('\d+', s) print(res) print(res.group())
<_sre.SRE_Match object; span=(0, 3), match='123'>
123
s1 = 'abc324asdfk234lkjsf324lkj' print(re.split('\d+', s1))
['abc', 'asdfk', 'lkjsf', 'lkj']
s1 = 'abc324asdfk234lkjsf324lkj' print(re.split('\d+', s1))
abc***asdfk***lkjsf***lkj
s1 = 'abc324asdfk234lkjsf324lkj' print(re.sub('\d+', '***', s1))
('abc***asdfk***lkjsf***lkj', 3)
s = 'abc123edf456' res = re.search('abc(?P<abc>\d+)edf(?P<edf>\d+)', s) print(res.groupdict())
{'abc': '123', 'edf': '456'}
这个方法是Pattern类的工厂方法,用于将字符串形式的正则表达式编译为Pattern对象。 第二个参数flag是匹配模式,取值可使用按位或运算符'|'表示同时生效,好比re.I | re.M。另外,你也能够在regex字符串中指定模式,好比re.compile('pattern', re.I | re.M)与re.compile('(?im)pattern')是等价的。
下表是全部的正则匹配模式:
修饰符 | 描述 |
---|---|
re.I | 使匹配对大小写不敏感 |
re.L | 作本地化识别(locale-aware)匹配 |
re.M | 多行匹配,影响 ^ 和 $ |
re.S | 使 . 匹配包括换行在内的全部字符 |
re.U | 根据Unicode字符集解析字符。这个标志影响 \w, \W, \b, \B. |
re.X | 该标志经过给予你更灵活的格式以便你将正则表达式写得更易于理解。 |
import re a = '''asdfhellopass: worldaf ''' b = re.findall('hello(.*?)world', a) c = re.findall('hello(.*?)world', a, re.S) print('b is ', b) print('c is ', c) b is [] c is ['pass:\n ']
正则表达式中,“.”的做用是匹配除“\n”之外的任何字符,也就是说,它是在一行中进行匹配。这里的“行”是以“\n”进行区分的。a字符串有每行的末尾有一个“\n”,不过它不可见。
若是不使用re.S参数,则只在每一行内进行匹配,若是一行没有,就换下一行从新开始,不会跨行。而使用re.S参数之后,正则表达式会将这个字符串做为一个总体,将“\n”当作一个普通的字符加入到这个字符串中,在总体中进行匹配。
res = re.findall(r"A", "abc", re.I) print(res) ['a']
s = '12 34/n56 78/n90' re.findall(r'^/d+', s, re.M) # 匹配位于行首的数字 # ['12', '56', '90'] re.findall(r'/A/d+', s, re.M) # 匹配位于字符串开头的数字 # ['12'] re.findall(r'/d+$', s, re.M) # 匹配位于行尾的数字 # ['34', '78', '90'] re.findall(r'/d+/Z', s, re.M) # 匹配位于字符串尾的数字 # ['90']
# 要求结果:['12', '23', '34'] l = ['1 2 ', '2 3', ' 3 4'] import re print(eval(re.sub(r'\s*', '', str(l)))) ['12', '23', '34']