我需要找一个带正则表达式的文本,让我们说“andres”,但它不能介于[]
例如,如果文字是:
s = 'andres [andres andres] andres [andres] andresX andres'
我应该得到第一,第四,第六和最后一个,其他人至少有一个[]
所以它们不匹配。
我试过了:
"[^\[]andres[^\]]"
一个更好的例子http://jsfiddle.net/aras7/5j3UM/8/
但它不起作用。
答案 0 :(得分:2)
在正则表达式中执行此类操作有一个有用的模式:
exclusion_context1|exclusion_context2|...|(stuff_you_want)
您可以根据需要指定任意数量的排除上下文,最后在捕获组中捕获您 想要的内容。我可以进一步解释,但实际上我只会link you to this answer深入了解上述模式。
那么,那么:
\[.*?\]|(andres)
我们的排除上下文懒惰地匹配括号内的任何内容,否则我们会捕获该上下文之外的所有andres
。
因为我刚刚注意到你想要匹配的位置,所以它在python中可能看起来像这样:
for m in re.finditer(r'\[.*?\]|(andres)', s):
if m.group(1):
print('{}: {}'.format(m.start(),m.group()))
0: andres
23: andres
39: andres
47: andres
答案 1 :(得分:1)
试试这个: 测试字符串是:
$string = 'andres [an1dres an1dres] andres [an1dres] andresX andres' ;
$patern = '/\\[.*?\\]| /';
答案 2 :(得分:1)
答案 3 :(得分:0)
尝试:
andres(?=[^[\]]+(?:\[|$))
解释
查找字符串后跟
的任何实例RegexBuddy的解释:
andres not between [ ... ]
andres(?=[^[\]]+(?:\[|$))
Options: Case sensitive; ^$ match at line breaks
Match the character string “andres” literally (case sensitive) «andres»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=[^[\]]+(?:\[|$))»
Match any single character NOT present in the list below «[^[\]]+»
Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
The literal character “[” «[»
The literal character “]” «\]»
Match the regular expression below «(?:\[|$)»
Match this alternative (attempting the next alternative only if this one fails) «\[»
Match the character “[” literally «\[»
Or match this alternative (the entire group fails if this one fails to match) «$»
Assert position at the end of a line (at the end of the string or before a line break character) (line feed, line feed, line separator, paragraph separator) «$»
Created with RegexBuddy