正则表达式排除特定字符串

我需要一个.NET正则表达式来匹配"[@foo]""applicant_data/contact_number[@foo]"瓦特/ c我已经可以使用模式"\[@(.*?)\]"。正则表达式排除特定字符串

但是,我想要例外,以使"applicant_data/contact_number[@foo=2]"与模式不匹配。所以问题是正则表达式应该是什么,这样它才能得到任何有效的字母数字([@bar],[@ theVar],[@ zoooo $ 6]),而不是[@bar = 1],[@ theVar = 3] ?

回答:

你可以试试这个:

\[@[\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

希望这会有所帮助!

回答:

试试这个正则表达式:

\[@(?![^\]]*?=).*?\]

Click for Demo

说明:

  • \[@ - 匹配[@字面上
  • (?![^\]]*?=) - 负前瞻,以确保=不下一]
  • .*?之前的任何地方出现 - 任何字符匹配0+出现除换行符
  • \] - 匹配]字面上

以上是 正则表达式排除特定字符串 的全部内容, 来源链接: utcz.com/qa/262194.html

回到顶部