Python 正则表达式匹配以baseUrl开头的字符串
您可以使用正则表达式的 '^' 符号来匹配以 'baseUrl://' 开头的字符串。下面是一个示例代码:
import re
data = """
http://www.example.com
https://www.example.com
http://www.example.com/page1
baseUrl://www.example.com
baseUrl://www.example.com/page2
"""
pattern = r'^baseUrl://.*'
matches = re.findall(pattern, data, re.MULTILINE)
for match in matches:
print(match)
输出结果为:
baseUrl://www.example.com
baseUrl://www.example.com/page2
在上述代码中,我们使用了 re.findall() 函数来查找所有匹配的字符串。其中,pattern 是我们定义的正则表达式模式,^ 表示匹配字符串的开头,baseUrl:// 表示具体的字符串内容,.* 表示匹配任意字符任意次数。re.MULTILINE 参数用于指定多行模式,以便能够匹配多行数据。最后,我们通过遍历匹配结果来输出所有匹配到的字符串。
原文地址: http://www.cveoy.top/t/topic/fbL5 著作权归作者所有。请勿转载和采集!