python正则表达式使用实例有哪些?

 我来答
见佛有真如c
高粉答主

2021-09-09 · 说的都是干货,快来关注
知道小有建树答主
回答量:4743
采纳率:100%
帮助的人:87.5万
展开全部

具体如下:

 1、测试正则表达式是否匹配字符串的全部或部分regex=ur"" #正则表达式:if re.search(regex, subject): do_something()else: do_anotherthing() 。

2、测试正则表达式是否匹配整个字符串 regex=ur"/Z" #正则表达式末尾以/Z结束:if re.match(regex, subject): do_something()else: do_anotherthing() 。

3、创建一个匹配对象,然后通过该对象获得匹配细节(Create an object with details about how the regex matches (part of) a string) regex=ur"" #正则表达式:

match = re.search(regex, subject)if match: # match start: match.start() # match end (exclusive): atch.end() # matched text: match.group() do_something()else: do_anotherthing()。

4、获取正则表达式所匹配的子串(Get the part of a string matched by the regex) regex=ur"" #正则表达式:match = re.search(regex, subject)if match: result = match.group()else: result ="" 。

5、获取捕获组所匹配的子串(Get the part of a string matched by a capturing group) regex=ur"" #正则表达式:match = re.search(regex, subject)if match: result = match.group(1)else: result ="" 。

推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式