首页 > 解决方案 > 使用正则表达式进行组提取

问题描述

我正在 Python 3 中学习 RegEx,并且使用括号来提取组给了我一个意想不到的行为,我无法在任何地方找到解释。

这是代码:

str = '<b>bold</b>'
match = re.search(r'>(\w+?)<', str)
match.group() == '>bold<'

我尝试了以下变体

match = re.search(r'>(.+?)<', str)
match = re.search(r'>(.+)<', str)
match = re.search(r'>(,)<', str)
match = re.search(r'>([\w]+)<', str)

他们都返回相同的字符串。据我所知,它应该只返回“粗体”。有人可以解释我做错了什么吗?

谢谢!

标签: regexpython-3.xexpressionextraction

解决方案


推荐阅读