![](/img/trans.png)
[英]C# Regular Expression regex to match characters in last group, but not empty group
[英]c# regex match a group of characters that not repeates
受到其他問題的啟發(我已經接受了非正則表達式解決方案) c#正則表達式以任意順序僅匹配一次字符集
但是@Dmitry Egorov的解決方案到目前為止還比較優雅,我仍在努力正確地解決它(如果可以使用一個正則表達式解決的話)。
^(.|\n)*<\[SG (?!.*(.).*\2)[msbrelft]+\]>(.|\n)*$
文本應匹配如下
ID-CFI Location 02h displays sector protection status for the sector selected by the sector address (SA) used in the ID-CFI enter
command. To read the protection status of more than one sector it is necessary to exit the ID ASO and enter the ID ASO using the
new SA. <[SG sbl]>
Page mode read between ID locations other than 02h is supported.
我在C#中使用此檢查
if (!Regex.IsMatch(obj.Object_Text, format.Value))
...
...
換句話說,匹配項應為:
- if this exists anywhere in text <[SG sbl]> including over \n or \r\n
- letters should be in this group of letters [msbrelft]
- must be minimum one letter, eg. <[SG s]>
- can be up to all from group, eg. <[SG sbl]>
- must be only one letter (no duplicates), eg. <[SG sbsl]> is NOT good
我不想提取組,只需使用先前解釋的規則驗證所有文本是否包含<[SG xx ..]>。
現在我已經想起來讓我發瘋了,
^(.|\n)*<\[SG (?!.*(.).*\2)[msbrelft]+\]>(.|\n)*$
在我感興趣的組之后,如果同一行上沒有兩個字母(不是\\ r \\ n或\\ n),則無法驗證。
因此,例如,這可行(組后有\\ n或\\ r \\ n)
ID-CFI Location 02h displays sector protection status for the sector selected by the sector address (SA) used in the ID-CFI enter
command. To read the protection status of more than one sector it is necessary to exit the ID ASO and enter the ID ASO using the
new SA. <[SG sbl]>
Page mode read between ID locations other than 02h is supported.
而不是(我組后面兩個空格)
ID-CFI Location 02h displays sector protection status for the sector selected by the sector address (SA) used in the ID-CFI enter
command. To read the protection status of more than one sector it is necessary to exit the ID ASO and enter the ID ASO using the
new SA. <[SG sbl]> Page mode read between ID locations other than 02h is supported.
任何幫助將不勝感激! 謝謝。
第一件事,如果您只想用規則找到一個<SG xxx>
來驗證字符串,則無需在模式中描述完整的字符串。
模式的問題在於,否定的前瞻可以檢查方括號定界的子字符串之外的字符,為避免此問題,您需要使用否定的字符類來更改點,該類不包括右方括號:
<\[SG (?![^\]]*([^\]])[^\]]*\1)[msbrelft]+\]>
您也可以這樣寫:
<\[SG (?:([msbrelft])(?![^\]]*?\1))+\]>
用[\\S\\s]*
替換(.|\\n)*
[\\S\\s]*
似乎可行。
\\ S:任何非空格
\\ s:空格,制表符,換行符...
^[\S\s]*<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>[\S\s]*$
此外,避免重復的否定前瞻現在使用\\w
代替.
。
由於]
不是單詞字符,因此它不會搜索超出該范圍的字符。
\\ w:單詞字符。
或者,就像Wiktor指出的那樣,將RegexOptions.Singleline
傳遞給regex構造函數,然后將regex編碼為:
^.*<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>.*$
無論如何,在另一個答案中,我注意到您確實只想搜索該SG標簽,而不是在包含標簽的情況下獲取整個文本。
所以最后,這將做:
<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.