Python匹配在我们使用的时候有很多的注意事项。我们在不断的学习中会遇到不少的问题。下面我们就详细的看看如何才能更好的掌握相关的Python匹配技术问题。用法2的正则表达式对象版本
rereobj = re.compile(r"\Z") #正则表达式末尾以\Z 结束
if reobj.match(subject):
do_something()
else:
do_anotherthing()
- 1.
- 2.
- 3.
- 4.
- 5.
创建一个正则表达式对象,然后通过该对象获得Python匹配细节
rereobj = re.compile(regex)
match = reobj.search(subject)
if match:
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group()
do_something()
else:
do_anotherthing()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
用正则表达式对象获取Python匹配子串
rereobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group()
else:
result = ""
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
用正则表达式对象获取 捕获组所Python匹配的子串
rereobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group(1)
else:
result = ""
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
用正则表达式对象获取 有名组所Python匹配的子串
rereobj = re.compile(regex)
match = reobj.search(subject)
if match:
result = match.group("groupname")
else:
result = ""
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
用正则表达式 对象获取所有Python匹配子串并放入数组
rereobj = re.compile(regex)
result = reobj.findall(subject)
- 1.
- 2.
通过正则表达式对象遍历所Python有匹配子串
rereobj = re.compile(regex)
for match in reobj.finditer(subject):
# match start: match.start()
# match end (exclusive): match.end()
# matched text: match.group
- 1.
- 2.
- 3.
- 4.
- 5.
以上就是对Python匹配的相关细节介绍。
【编辑推荐】