我想创建一个正则表达式,它可以执行以下操作:
addpaths
、addpath
、test
%
符号开头的行'
和"
包围的匹配项所以我提出了以下正则表达式(带有标志g
,m
):
^[^%]*?(?<=[^\'\"])\b(addpaths|addpath|test)\b(?=[^\'\"]).*?$?
这给了我以下结果(见regex101):
function addpaths() --> match, correct
% function addpaths to add paths to path --> no match, correct
fprintf('running addpaths') --> no match, correct
fprintf('addpaths running') --> no match, correct
fprintf('running addpaths.') --> match, wrong
fprintf('running addpaths function') --> match, wrong
% fprintf('running addpaths') --> no match, correct
% fprintf('addpaths running') --> no match, correct
% fprintf('running addpaths function') --> no match, correct
% test what happens to 'test' --> no match, correct
run('test') --> no match, correct
'this is a test.' --> match, wrong
test --> match, correct
所以正则表达式在一个完全匹配的单词紧挨着一个'
的时候起作用,但当它旁边有另一个单词,空格或.
的时候就不起作用了。为什么
import re
text = '''function addpaths()
% function addpaths to add paths to path
fprintf('running addpaths')
fprintf('addpaths running')
fprintf('running addpaths function')
% fprintf('running addpaths')
% fprintf('addpaths running')
% fprintf('running addpaths function')
% test what happens to 'test'
run('test')
'this is a test.'
test
'''
pattern = '^[^%]*?(?<=[^\'\"])\\b(addpaths|addpath|test)\\b(?=[^\'\"]).*?$'
regex = re.compile(pattern, re.M)
matches = regex.findall(text)
for m in matches:
print(m)
试试这个:
输出:
我使用
negative lookahead
(?!.*?['\"])
是因为'this is a test.'
在单词test
后面有.
,但是在你regex
(addpaths|addpath|test)\b(?=[^\'\"])
中,你排除了直接跟在引号后面的文本。这就是为什么这个run('test')
不起作用的原因相关问题 更多 >
编程相关推荐