我需要一个.NET正则表达式来匹配来自"[@foo]"
的{{1}} w / c我已经可以使用模式"applicant_data/contact_number[@foo]"
进行匹配。
但是我想做一个例外,以便"\[@(.*?)\]"
与模式不匹配。所以问题是正则表达式应该是什么,以便它将获得任何有效的字母数字([@ bar],[@ theVar],[@ zoo $ 6])但不是[@ bar = 1],[@ theVar = 3] ?
答案 0 :(得分:1)
试试这个正则表达式:
\[@(?![^\]]*?=).*?\]
<强>解释强>
\[@
- 按字面意思匹配[@
(?![^\]]*?=)
- 负面预测确保=
在下一个]
之前的任何地方都不存在.*?
- 匹配除换行符之外的任何字符的出现次数\]
- 按字面意思匹配]
答案 1 :(得分:1)
你可以试试这个:
\[@[\w-]+(?!=)\]
解释:
"\[" & ' Match the character “[” literally
"@" & ' Match the character “@” literally
"[\w-]" & ' Match a single character present in the list below
' A “word character” (Unicode; any letter or ideograph, digit, connector punctuation)
' The literal character “-”
"+" & ' Between one and unlimited times, as many times as possible, giving back as needed (greedy)
"(?!" & ' Assert that it is impossible to match the regex below starting at this position (negative lookahead)
"=" & ' Match the character “=” literally
")" &
"\]" ' Match the character “]” literally
希望这有帮助!