re.match(匹配的规则 ,被匹配的字符串)
注意此方法是从被匹配的字符串的开头进行匹配的,如果匹配成功,返回匹配对象(包含匹配的信息),匹配不成功返回为空None。
ximport re txt: str = "python test" # 被匹配的字符串pattern: str = "python" # 匹配的规则 result = re.match(pattern, txt)print(result)print(result.span())print(result.group())
import re
txt: str = "python test" # 被匹配的字符串
pattern: str = "python" # 匹配的规则
result = re.match(pattern, txt)
print(result)
print(result.span())
print(result.group())
xxxxxxxxxx输出结果:<re.Match object; span=(0, 6), match='python'>(0, 6)python
xxxxxxxxxx
输出结果:
<re.Match object; span=(0, 6), match='python'>
(0, 6)
python
xxxxxxxxxximport re txt: str = "test python python test" # 被匹配的字符串pattern: str = "python" # 匹配的规则 result = re.match(pattern, txt)print(result)
txt: str = "test python python test" # 被匹配的字符串
xxxxxxxxxx输出结果:None
None
上述代码重新整理如下:
xxxxxxxxxximport re txt: str = "python python test" # 被匹配的字符串pattern: str = "python" # 匹配的规则 result = re.match(pattern, txt)if result is None: print("匹配不成功")else: print(result) print(result.span()) print(result.start()) print(result.end()) print(result.group())
txt: str = "python python test" # 被匹配的字符串
if result is None:
print("匹配不成功")
else:
print(result.start())
print(result.end())
正则表达式付费代写、咨询、答疑解惑,专业、快速、高效帮您解决正则表达式方面的各种问题,可定制各类软件应用程序。
正则学习工作必备在线工具合集
.
^
$
\d
\w
\s
\D
\W
\S
[abc]
[a-z]
[^abc]
aa|bb
?
*
+
{n}
{n,}
{m,n}
(
)
\1
(?:
(?=
(?!
在线客服QQ:543690914,备案号: 苏ICP备15037649号-32。东海县白塔埠镇佳诚电脑经营部版权所有。