Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Regex-在字符串中多次匹配_C#_Regex_Rosalind - Fatal编程技术网

C# Regex-在字符串中多次匹配

C# Regex-在字符串中多次匹配,c#,regex,rosalind,C#,Regex,Rosalind,我正在尝试在'NNTSY'上进行正则表达式搜索,以便获得两个匹配项 NNTS NTSY 当我尝试使用模式?N[^p][ST][^p])“进行匹配时,我只得到一个匹配项,即NNTS 如何使用正则表达式匹配NNTSY,以便找到两个匹配项 注:背景信息:可以找到Rosalind问题 这是我的密码 input = "NNTSY"; Regex regex = new Regex("(?<NGrlyosylation>N[^P][ST][^P])", Reg

我正在尝试在'NNTSY'上进行正则表达式搜索,以便获得两个匹配项

  • NNTS
  • NTSY
当我尝试使用模式
?N[^p][ST][^p])“
进行匹配时,我只得到一个匹配项,即
NNTS

如何使用正则表达式匹配
NNTSY
,以便找到两个匹配项

注:背景信息:可以找到Rosalind问题

这是我的密码

        input = "NNTSY";
        Regex regex = new Regex("(?<NGrlyosylation>N[^P][ST][^P])", RegexOptions.Compiled | RegexOptions.IgnoreCase);
        MatchCollection matches = regex.Matches(input);
        foreach (Match match in matches)
        {
            // Need to add 1 to because match index is 0 based
            const int offset = 1;
            yield return match.Index + offset;
        }
input=“NNTSY”;
Regex Regex=new Regex((?N[^P][ST][^P]),RegexOptions.Compiled | RegexOptions.IgnoreCase);
MatchCollection matches=regex.matches(输入);
foreach(匹配中的匹配)
{
//需要将1添加到,因为匹配索引基于0
常数int offset=1;
收益率-收益率匹配。指数+抵销;
}

大多数编程语言通常不允许查找重叠的匹配项(少数语言除外)。 因此,我认为不存在一种纯粹的正则表达式方法来解决这个问题,但是您可以在C中使用
子字符串
,并使用
前瞻
作为

(?=N[^P][ST][^P]).
C#代码


您正在尝试查找重叠matches@rock321987是的。没错。对于那些想要完整源代码的人,请在这里查看源代码=>谢谢@rock321987。我不知道Regex
lookahead
的概念,所以我迷路了。至少我创建的测试通过了所有案例,我准备继续前进。@Sung很高兴,这很有帮助。
lookaheads
是零宽度断言。这意味着它们不使用任何字符。将对照字符串检查正则表达式,而不检查匹配的字符串consumed@Sung
在正则表达式中不是必需的。.这里只需简单的前瞻即可。.添加
将稍微加快处理速度
string input = "NNTSY";
Regex regex = new Regex("(?=N[^P][ST][^P]).", RegexOptions.Compiled | RegexOptions.IgnoreCase);

Match match = regex.Match(input);

while (match.Success)
{
    Console.WriteLine(input.Substring(match.Index, 4));
    match = match.NextMatch();
}