我正在实现一个正则表达式模式到一个字符串列表。 这样一个字符串的例子是: "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]"))) : nullI 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()) : nullEDIT: 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()) : nullI 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.
更多推荐
发布评论