如何完全抛弃不匹配正则表达式模式的字符串的发生?(How to completely discard the occurrence of a string not matching the regex

编程入门 行业动态 更新时间:2024-10-22 08:28:06
如何完全抛弃不匹配正则表达式模式的字符串的发生?(How to completely discard the occurrence of a string not matching the regex pattern?)

我正在实现一个正则表达式模式到一个字符串列表。 这样一个字符串的例子是: "MNT-PUT-Y0-HAS90" 。 还有其他UNWANTED字符串,例如: "MNT-PUT-HAS90" 。

当我执行下面的代码时,我得到了不需要的代码,我猜想Regex是如何工作的。 而且,我得到了"MNT-PUT-Y0-HAS90" 。

问题是:我怎样才能完全忽略MNT-PUT-HAS90的发生。 我只想检索字符串结果 - "MNT-PUT-Y0-HAS90" 。

我已经为此执行了下面的代码:

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk.Select(b => b.Store). Select(x => Regex.Match(x, "[A-Z]+-[A-Z]+-T[0-9]-[A-Z]+[0-9]"))) : null

我尝试将代码更改为下面的代码,但它向我显示一个错误: "Cannot convert lambda expression to the intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type"

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk.Select(b => b.Store). Where(x => Regex.Match(x, "[A-Z]+-[A-Z]+-T[0-9]-[A-Z]+[0-9]")).ToString()) : null

编辑:只是试过这个:

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk.Select(b => b.Store). Where(x => Regex.IsMatch(x, "[A-Z]+-[A-Z]+-T[0-9]-[A-Z]+[0-9]")).ToList()) : null

我没有得到任何错误,但也没有得到所需的输出。

I am implementing a regex pattern to a list of strings. Example of such a string is:"MNT-PUT-Y0-HAS90". There are other UNWANTED strings as well, like: "MNT-PUT-HAS90".

When I execute the below code, I get "" for the unwanted one, which I guess how Regex works. And, I get "MNT-PUT-Y0-HAS90" for the wanted one.

The question is: How can I completely ignore the occurrence of MNT-PUT-HAS90. I want to retrieve results for string - "MNT-PUT-Y0-HAS90" only.

I have implemented the below code for this:

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk.Select(b => b.Store). Select(x => Regex.Match(x, "[A-Z]+-[A-Z]+-T[0-9]-[A-Z]+[0-9]"))) : null

I tried changing the code to the below, but it shows me an error: "Cannot convert lambda expression to the intended delegate type because some of the return types in the block are not implicitly convertible to the delegate return type"

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk.Select(b => b.Store). Where(x => Regex.Match(x, "[A-Z]+-[A-Z]+-T[0-9]-[A-Z]+[0-9]")).ToString()) : null

EDIT: Just tried this:

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk.Select(b => b.Store). Where(x => Regex.IsMatch(x, "[A-Z]+-[A-Z]+-T[0-9]-[A-Z]+[0-9]")).ToList()) : null

I get no error but do not get the desired output either.

最满意答案

你需要使用

Regex.IsMatch(x, "^[A-Z]+-[A-Z]+-[A-Z]+[0-9]+-[A-Z]+[0-9]+$")

看看这个正则表达式是如何工作的

细节

^ - 字符串的开头 [AZ]+ - 1+ ASCII大写字母 - - hypehn [AZ]+- - - 1+ ASCII大写字母和连字符 [AZ]+[0-9]+- - 1+ ASCII大写字母,1+ ASCII数字,然后连字符 [AZ]+[0-9]+ - 1+ ASCII大写字母,1+ ASCII数字 $ - 字符串的结尾。

码:

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk .Select(b => b.Store) .Where(x => Regex.IsMatch(x, "^[A-Z]+-[A-Z]+-[A-Z]+[0-9]+-[A-Z]+[0-9]+$")) ) : null;

如果预期匹配在更长的字符串内的任何位置,请删除^和$锚。

You need to use

Regex.IsMatch(x, "^[A-Z]+-[A-Z]+-[A-Z]+[0-9]+-[A-Z]+[0-9]+$")

See how this regex works.

Details

^ - start of the string [A-Z]+ - 1+ ASCII uppercase letters - - a hypehn [A-Z]+- - - 1+ ASCII uppercase letters and a hyphen [A-Z]+[0-9]+- - 1+ ASCII uppercase letters, 1+ ASCII digits and then a hyphen [A-Z]+[0-9]+ - 1+ ASCII uppercase letters, 1+ ASCII digits $ - end of string.

Code:

Store = a.Type == "Machines" ? string.Join(",", a.Info.Disk .Select(b => b.Store) .Where(x => Regex.IsMatch(x, "^[A-Z]+-[A-Z]+-[A-Z]+[0-9]+-[A-Z]+[0-9]+$")) ) : null;

If the match is expected anywhere inside a longer string, remove ^ and $ anchors.

更多推荐

本文发布于:2023-07-25 06:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1257250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   不匹配   发生   模式   正则表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!