2024-09-21 02:55:37 发布
网友
我需要关于非常简单的问题的帮助,这是一个使用带有多行字符串的正则表达式的条件问题。我不明白为什么这不起作用:
if(re.match(r"\w", " \n\n\n aaaaaaaaaaaa\n\n", re.MULTILINE)): print('ok') else: print('fail') fail
我希望结果是正常的,但是没有匹配的数据。我尝试使用https://regex101.com/r/BsdymE/1,但在我的代码中有工作,没有工作
仅当搜索字符串位于开头时,re.match才会返回匹配
https://docs.python.org/3/library/re.html#re.match
re.match(pattern, string, flags=0)If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.
re.match(pattern, string, flags=0)
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern; note that this is different from a zero-length match.
尝试改用re.search(pattern, string, flags=0)
re.search(pattern, string, flags=0)
从pydoc re.match
pydoc re.match
Try to apply the pattern at the start of the string, returning a Match object, or None if no match was found.
(我的重点)。因此,问题不在于字符串是多行的,而在于它不是以单词类字符开头的。如果要检查字符串是否包含任何内容,请使用re.search
re.search
仅当搜索字符串位于开头时,re.match才会返回匹配
https://docs.python.org/3/library/re.html#re.match
尝试改用
re.search(pattern, string, flags=0)
从
pydoc re.match
(我的重点)。因此,问题不在于字符串是多行的,而在于它不是以单词类字符开头的。如果要检查字符串是否包含任何内容,请使用
re.search
相关问题 更多 >
编程相关推荐