首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >c#正则表达式匹配一组不重复的字符。

c#正则表达式匹配一组不重复的字符。
EN

Stack Overflow用户
提问于 2017-04-20 10:38:52
回答 2查看 266关注 0票数 1

受到其他问题的启发(我已经接受了一个非正则表达式解决方案) c# regex match set of characters in any order only once

但是@Dmitry Egorov的这个解决方案要优雅得多,我仍然在努力解决这个问题(如果可以用一个正则表达式来解决),我得到的最接近的就是这个。

代码语言:javascript
复制
^(.|\n)*<\[SG (?!.*(.).*\2)[msbrelft]+\]>(.|\n)*$

应该匹配的文本如下

代码语言:javascript
复制
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#中使用这张支票

代码语言:javascript
复制
if (!Regex.IsMatch(obj.Object_Text, format.Value))
...
...

从文字上说,匹配应该是:

代码语言:javascript
复制
- 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

我不想提取组,只要验证所有文本,如果包含与先前解释的规则。

现在我已经想起来把我逼疯了,

代码语言:javascript
复制
^(.|\n)*<\[SG (?!.*(.).*\2)[msbrelft]+\]>(.|\n)*$

如果在我感兴趣的组之后,一行上有两个字母(no \r\n或\n),则不进行验证。

因此,例如,这是有效的(有一个\n或\r\n后组)

代码语言:javascript
复制
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.

但这不是(我的小组后面有两个空格)

代码语言:javascript
复制
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.

任何帮助都将不胜感激!谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-04-20 11:26:08

首先,如果您只想找到一个包含规则的<SG xxx>来验证字符串,则不需要在模式中描述完整的字符串。

模式的问题是,您的负查找可以检查方括号分隔子字符串之外的字符,以避免出现不包含结束方括号的负字符类更改点的问题:

代码语言:javascript
复制
<\[SG (?![^\]]*([^\]])[^\]]*\1)[msbrelft]+\]>

你也可以这样写:

代码语言:javascript
复制
<\[SG (?:([msbrelft])(?![^\]]*?\1))+\]>
票数 1
EN

Stack Overflow用户

发布于 2017-04-20 11:32:26

(.|\n)*代替[\S\s]*似乎是可行的。

\S :任何不是空格的东西

空格,制表符,行提要,..。

代码语言:javascript
复制
^[\S\s]*<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>[\S\s]*$

此外,为了避免重复的负面展望,现在使用\w而不是.

因为]不是一个字字,所以它不会在它之外进行搜索。

W:字字。

或者,正如Wiktor所指出的,将RegexOptions.Singleline传递给regex构造函数,然后将regex编码为:

代码语言:javascript
复制
^.*<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>.*$

无论如何,从另一个答案中我注意到,您实际上只想搜索SG标记,而不是获取包含标记的整个文本。

所以最终,这样做是可以的:

代码语言:javascript
复制
<\[SG (?!\w*(\w)\w*\1)[beflmrst]+\]>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43517199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档