试图从字符串中提取字符串和数字,但未按预期执行
s = '''
text1
text2
http://url.com/bla1/blah1/text22
text3
'''
import re
re.findall(r'(^http.*|text\d+)',s)
我的想法是,我不会考虑这条线
['text1', 'text2', 'text22', 'text3']
预期
['text1', 'text2', 'text3']
免责声明
请不要做're.sub(r'http\S+', '', s)'
Tags:
我怀疑您试图在行中的任何地方找到所有
text\d+
,除了以http
开头的行(否则我想不出额外的http
检查是否只想在行的开头text
的原因)如果是这样,您可以使用:
^(?!http).*(text\d+)
。第一组是你需要的Python的
re.findAll
返回捕获组的内容:尝试使用这一行来检测它是否在字符串的开头,我添加了
re.MULTILINE
标志,以便它尝试匹配每一行:输出:
相关问题 更多 >
编程相关推荐