简体   繁体   English

正则表达式未给出所有可能的匹配项

[英]regex not giving all the possible matches

I need to get all the possible matches for a given regular expression and word in c#. 我需要获取给定正则表达式和c#中单词的所有可能匹配项。 But the Regex.Matches() function is not giving it. 但是Regex.Matches()函数没有提供它。 For eg. 例如。

Regex.Matches("datamatics","[^aeiou]a[^aeiou]")

returns only two matches which are 仅返回两个匹配项

dat
mat

it is not giving "tam" as a match. 它没有给出“ tam”作为匹配项。 Can somebody explain to me why it is not giving "tam" as a match and how can I get all the three? 有人可以向我解释为什么不给予“ tam”作为匹配,我怎么能得到这三个?

Use this regex 使用此正则表达式

(?<=([^aeiou]))a(?=([^aeiou]))

.net supports group capture in lookarounds..cheers .net在环视环境中支持组捕获。

Your code would be 您的代码将是

var lst= Regex.Matches(input,regex)
              .Cast<Match>()
              .Select(x=>x.Groups[1].Value+"a"+x.Groups[2].Value)
              .ToList();

Now you can iterate over lst 现在您可以遍历lst

foreach(String s in lst)
{
     s;//required strings
}

You can't get overlapping matches in Regex. 您无法在正则表达式中得到重叠的匹配项。 You have several ways to work around it, though. 不过,您有几种解决方法。 You can either use Regex.Match , and specify a starting index (use a loop to go through your whole string), or you can use lookbehinds or lookaheads, like so: 您可以使用Regex.Match并指定起始索引(使用循环遍历整个字符串),也可以使用lookbehinds或lookaheads,如下所示:

  (?=[^aeiou]a)[^aeiou]

This works because lookbehinds and lookaheads do not consume the characters. 之所以可行,是因为先行查找和先行查找不会占用字符。 It returns a Match which contains the index of the match. 它返回一个Match ,其中包含Match的索引。 You'd need to use that instead of captures, since only one character is captured. 由于只捕获了一个字符,因此您需要使用它而不是捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM