簡體   English   中英

正則表達式排除特定字符串

[英]Regex Exclude Specific String

我需要有一個.NET正則表達式匹配"[@foo]""applicant_data/contact_number[@foo]" W / C我已經可以做到使用模式"\\[@(.*?)\\]"

但是我想作一個例外,以便"applicant_data/contact_number[@foo=2]"與模式不匹配。 因此,問題是正則表達式應該是什么,以便它將獲得任何有效的字母數字([@bar],[@ theVar],[@ zoo $ 6]),而不是[@ bar = 1],[@ theVar = 3] ?

試試這個正則表達式:

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

點擊演示

說明:

  • \\[@ -匹配[@字面意思
  • (?![^\\]]*?=) -前瞻,以確保=在下一個]之前不存在=
  • .*? -匹配0+次出現的除換行符以外的任何字符
  • \\] -匹配]

您可以嘗試以下方法:

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

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM