2017-04-20 103 views
1

其他問題的啓發在這裏一組字符(我已經接受了非正則表達式的解決方案) c# regex match set of characters in any order only onceC#正則表達式匹配不repeates

但是從@Dmitry埃格羅夫該解決方案是目前更優雅,我仍然努力解決它正確(如果它可以用一個正則表達式來解決) 我得到的最接近的是這一個

^(.|\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. 

任何幫助將非常感謝! 謝謝。

+0

您是否在尋找有效的''子?像[this](http://regexstorm.net/tester?p=%3c%5c%5bSG%5cs%2b%28%3f!%5bmsbrelft%5d*%28%5bmsbrelft%5d%29%5bmsbrelft%5d* %5C1%29%5bmsbrelft%5D%2B%5D%3E與I =%3C%5bSG + SBL%5D%3E%0D 0A%。%3C%5bSG + SBSL%5D%3E)? –

+0

使用正則表達式的最佳方式是獲取正則表達式結果。然後在正則表達式結果上使用Distinct方法。當沒有重複時,正則表達式結果和不同結果將匹配。 – jdweng

回答

1

首先,如果您只想找到一個帶有規則驗證字符串的<SG xxx>,則無需在模式中描述完整的字符串。

使用圖案的問題是,你的負面先行可以檢查字符分隔子的方括號外面,避免你需要排除右方括號負字符類改變點問題:

<\[SG (?![^\]]*([^\]])[^\]]*\1)[msbrelft]+\]> 

你也可以寫這樣的:

<\[SG (?:([msbrelft])(?![^\]]*?\1))+\]> 
+1

謝謝先生!完善!它從昨天開始感到困擾,非常感謝。 – orfruit

1

更換(.|\n)*通過[\S\s]*似乎工作。
\ S:任何東西,這不是一個空白
\ S:空格,製表符,換行符,...

^[\S\s]*<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>[\S\s]*$ 

此外,負先行,避免重複現在使用\w,而不是.
由於]不是一個字符,它不會搜索超出它。
\ w:單詞字符。

或者,像Wiktor的人士指出,通過RegexOptions.Singleline的正則表達式構造函數和正則表達式可以golfcoded到:

^.*<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>.*$ 

無論如何,從對方的回答我注意到,你真的只是想以搜索SG標記,而不是在包含標記的情況下獲取整個文本。

所以在最後,這會做:

<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]> 
+2

不需要在C#中使用'[\ s \ S]',您可以將'RegexOptions.Singleline'標誌傳遞給Regex構造函數,'.'將匹配任何字符。那就是''*'夠了。 –

+0

謝謝你的努力! – orfruit

+0

@WiktorStribiżew謝謝你的提示:) – LukStorms