受到其他问题的启发(我已经接受了一个非正则表达式解决方案) c# regex match set of characters in any order only once。
但是@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我不想提取组,只要验证所有文本,如果包含与先前解释的规则。
现在我已经想起来把我逼疯了,
^(.|\n)*<\[SG (?!.*(.).*\2)[msbrelft]+\]>(.|\n)*$如果在我感兴趣的组之后,一行上有两个字母(no \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.任何帮助都将不胜感激!谢谢。
发布于 2017-04-20 11:26:08
首先,如果您只想找到一个包含规则的<SG xxx>来验证字符串,则不需要在模式中描述完整的字符串。
模式的问题是,您的负查找可以检查方括号分隔子字符串之外的字符,以避免出现不包含结束方括号的负字符类更改点的问题:
<\[SG (?![^\]]*([^\]])[^\]]*\1)[msbrelft]+\]>你也可以这样写:
<\[SG (?:([msbrelft])(?![^\]]*?\1))+\]>发布于 2017-04-20 11:32:26
用(.|\n)*代替[\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]+\]>https://stackoverflow.com/questions/43517199
复制相似问题