模式匹配和正则表达式_python

模式匹配和正则表达式的学习

python实现

正则表达式

其对文字处理有着超高的效率
利用正则表达式可以实现“三步解决一个问题”

步骤

  • 1.用 import re 导入正则表达式模块。
  • 2.用 re.compile()函数创建一个 Regex 对象(记得使用原始字符串)。
  • 3.向 Regex 对象的 search()方法传入想查找的字符串。它返回一个 Match 对象。
  • 4.调用 Match 对象的 group()方法,返回实际匹配文本的字符串。

正则表达式-regex

import re
# first step , write a expected module
phoneNumRegex=re.complie(r'\d\d\d-\d\d\d-\d\d\d\d')
# second step ,input the the string
mo=phoneNumRegex.search('my phone number is : 123-456-1123')
# third step ,output the group result
print('phone number found:'+mo.group())

其中 正则表达式的编写前 加一个r,是用来表示是原始字符串,不包括转义字符

use group separate the item

phoneNumRegex2=re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo2=phoneNumRegex2.search('my phone numer is:345-232-4556')
print('mo2.group(1):'+mo2.group(1))
print('mo2.group(2):'+mo2.group(2))
print("mo2.group(0):"+mo2.group(0))
print('mo2.group():'+mo2.group())
print(mo2.groups()) 

如果想匹配 ()括号在字符串中,使用'\(' ,'\)' 转义字符来实现

# explain ,if need () in regex  use '\(' 

# use | pipeline  match more than one string
heroRegex=re.compile(r'dengshuo|zhengchuan')
mo3=heroRegex.search('dengshuo orginal name is deng zhengchuan')
mo4=heroRegex.search('deng zhengchuan is dengshuo')
print(mo3.group())                                       # | just like or ,return the first match group 
print(mo4.group())                                       # can use findall() match all group


# use ? implement optinal match   ,? partment  0 or 1 times
batRgex=re.compile(r'Bat(wo)?man')
mo5=batRgex.search('dengshuo is a Batman')
mo6=batRgex.search('a woman can be a Batwoman')
print(mo5.group())
print(mo6.group())

# use * 0, 1 or many times
# use + ,1 or many times

batRgex=re.compile(r'Bat(wo)*man')       # can get Batman
batRgex=re.compile(r'Bat(wo)+man')       # can't  get Batman 

# use (wo){} match special number
batRgex=re.compile(r'Bat(wo){2}man')     # only match Batwowoman
batRgex=re.compile(r'Bat(wo){3,5}man')   # can match  Batwowowoman Batwowowowoman Batwowowowowoman

匹配多个相同字符时,默认是贪心的

greedyHaRegex = re.compile(r'(Ha){3,5}')        # 贪心策略
mo1 = greedyHaRegex.search('HaHaHaHaHa')
mo1.group()
## 'HaHaHaHaHa'
nongreedyHaRegex = re.compile(r'(Ha){3,5}?')   # 非贪心
mo2 = nongreedyHaRegex.search('HaHaHaHaHa') 
mo2.group()
## 'HaHaHa'

findall()

  • 1.如果调用在一个没有分组的正则表达式上

    例如\d\d\d-\d\d\d-\d\d\d\d,方法 findall()将返回一个匹配字符串的列表,例如['415-555-9999', '212-555-0000']。

  • 2.如果调用在一个有分组的正则表达式上

    例如(\d\d\d)-(\d\d\d)-(\d\d\d\d),方 法 findall()将返回一个字符串的元组的列表(每个分组对应一个字符串),例如[('415', '555', '1122'), ('212', '555', '0000')]。

自定义字符分类

\d   数字
\D   非数字
\w   字母,数字,下划线
\W  非(字母,数字,下划线)
\s    空格,制表符,换行符
\S   ^()

^  必须以什么字符串开始
$ 必须以什么字符串结束
.  通配符
* 重复字符
.* 可以用来匹配除去换行符外的所有字符

re.complie(r'[aeiouAEIOU]')

当然还有反向选择
re.complie(r'[^aeiouAEIOU]')

快速检索

  • ?匹配零次或一次前面的分组。
  • *匹配零次或多次前面的分组。
  • +匹配一次或多次前面的分组。
  • {n}匹配 n 次前面的分组。
  • {n,}匹配 n 次或更多前面的分组。
  • {,m}匹配零次到 m 次前面的分组。
  • {n,m}匹配至少 n 次、至多 m 次前面的分组。
  • {n,m}?或*?或+?对前面的分组进行非贪心匹配。
  • ^spam 意味着字符串必须以 spam 开始。
  • spam$意味着字符串必须以 spam 结束。
  • .匹配所有字符,换行符除外。
  • \d、\w 和\s 分别匹配数字、单词和空格。
  • \D、\W 和\S 分别匹配出数字、单词和空格外的所有字符。
  • [abc]匹配方括号内的任意字符(诸如 a、b 或 c)。
  • [^abc]匹配不在方括号内的任意字符。

猜你喜欢

转载自www.cnblogs.com/GeekDanny/p/10843652.html