Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式搜索,不以任何指定字母开头和结尾_Python_Regex - Fatal编程技术网

python正则表达式搜索,不以任何指定字母开头和结尾

python正则表达式搜索,不以任何指定字母开头和结尾,python,regex,Python,Regex,我想在一个句子中找到“foo”,在“foo”的开头和结尾没有任何小写、大写字符、数字或下划线 例如,在“.foo-bar(foo)bar foo-bar foo\u-bar foo'bar foo-bar foo-bar foo-bar,foo.”中,我想找到“.foo”,,“foo-”,“foo”,“-foo”,,“foo.” 我试图通过使用这个正则表达式来解决这个问题:“(?![a-zA-Z0-9])foo(?![a-zA-Z0-9])”;但是它不起作用。在字符类的开头使用^: >&g

我想在一个句子中找到“foo”,在“foo”的开头和结尾没有任何小写、大写字符、数字或下划线

例如,在
“.foo-bar(foo)bar foo-bar foo\u-bar foo'bar foo-bar foo-bar foo-bar,foo.”
中,我想找到
“.foo”
“foo-”,
“foo”
“-foo”
“foo.”


我试图通过使用这个正则表达式来解决这个问题:
“(?![a-zA-Z0-9])foo(?![a-zA-Z0-9])”
;但是它不起作用。

在字符类的开头使用
^

>>> strs = ".foo bar (foo) bar foo-bar foo_bar foo'bar bar-foo bar, foo."
>>> re.findall('[^\w+]foo[^\w+]', strs)
['.foo ', '(foo)', ' foo-', " foo'", '-foo ', ' foo.']

看看消极的向前看/向后看:)@SaulloCastro:是的,它是这样的;它工作正常,并且符合该字符串的要求。