2017-12-18 83 views
0

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

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

+0

你试过了什么? – Sefe

+0

\ [@(。*?)[^ = 2] \] – Sunil

+0

单词“foo”是常量还是可以变化?另外,你只想为数字'2'或任何数字排除它? – Gurman

回答

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 

希望这会有所帮助!

+0

任何机会,你可以扩大答案,只有从” @ [foo]“意思是找到一个单词字符(不带”=“符号),后面跟着”@ [“,后面跟着”]“ – james

+0

确定...''(?<= @ \ [)\ w +(? '?(?<= @ \ [\ w \ w +(?)] \\]]''... – Cylian

+0

谢谢@Cylian不工作,虽然 – james

1

试试这个正则表达式:

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

Click for Demo

说明:

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

谢谢@Gurman“= 2”不是常量,它可能是“= bar”。坦率地说,只要检测“[@Word]”中的“=”符号的存在就行。 – james

+0

看起来像'\ [@(?![^ \]] *?=)。*?\]“将工作 – james

+0

@james解决方案已更新 – Gurman

相关问题