2017-12-18 86 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

相關問題