我想在'NNTSY`上做一個正則表達式搜索,這樣我就可以得到兩個匹配。正則表達式 - 在一個字符串中多次匹配
- NNTS
- NTSY
當我試圖使用模式?<NGrlyosylation>N[^P][ST][^P])"
匹配,我只得到一個匹配,這是NNTS
。
如何使用正則表達式匹配NNTSY
以便找到兩個匹配項?
注意:背景信息:Rosalind問題可以找到here。
這是我的代碼。
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;
}
你正試圖找到重疊匹配 – rock321987
@ rock321987是的。究竟。 – Sung
對於那些想要完整源代碼的人,請查看這裏的源代碼=> https://github.com/dance2die/Demo.Rosalind/blob/master/Demo.Rosalind/Demo.Rosalind.Tests/MPRT/MprtTest.cs – Sung