Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我是正则表达式的新手,只想知道是否有可能在匹配中找到“重叠”组。
假设以下字符串:
20122 0029431 7094 0111 5890
我现在想要所有匹配项:4number+space+4number+space+4number
我尝试的是这样的:[0-9]{4}[\s][0-9]{4}[\s][0-9]{4}
[0-9]{4}[\s][0-9]{4}[\s][0-9]{4}
但这只是给了我:9431 7094 0111
我想要的是这些比赛:
这可以用正则表达式吗?
是的,如果您将前瞻断言与捕获组结合使用:
Regex regexObj = new Regex(@"(?=(\d{4}\s\d{4}\s\d{4}))"); Match matchResult = regexObj.Match(subjectString); while (matchResult.Success) { resultList.Add(matchResult.Groups[1].Value); matchResult = matchResult.NextMatch(); }