防止正则表达式中的重复匹配
以下代码
string expression = "(\\{[0-9]+\\})";
RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
Regex tokenParser = new Regex(expression, options);
MatchCollection matches = tokenParser.Matches("The {0} is a {1} and the {2} is also a {1}");
将匹配并捕获“{0}”、“{1}”、“{2}”和“{1}”。
是否可以更改它(正则表达式或正则表达式的选项),以便它匹配并捕获“{0}”、“{1}”和“{2}”。换句话说,每场比赛只能被捕获一次?
The following code
string expression = "(\\{[0-9]+\\})";
RegexOptions options = ((RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline) | RegexOptions.IgnoreCase);
Regex tokenParser = new Regex(expression, options);
MatchCollection matches = tokenParser.Matches("The {0} is a {1} and the {2} is also a {1}");
will match and capture "{0}", "{1}", "{2}" and "{1}".
Is it possible to change it (either the regular expression or option of the RegEx) so that it would match and capture "{0}", "{1}" and "{2}". In other words, each match should only be captured once?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我想出的。
请注意,正则表达式与我的问题中的正则表达式的差异是由于我满足两种类型的标记这一事实;编号由 {} 分隔,命名由 [] 分隔;
Here is what I came up with.
Note that the difference in the regular expression from the one in my question is due to the fact that I cater for two types of token; numbered ones delimited by {} and named ones delimited by [];
正则表达式可以解决很多问题,但不是所有问题。使用工具箱中的其他工具怎么样?
或者
Regular expressions solve lots of problems, but not every problem. How about using other tools in the toolbox?
Or
您可以将以下内容用于纯正则表达式解决方案:
但是为了提高效率和可维护性,您可能最好使用像您发布的那样的混合解决方案。
Here's something you could use for a pure regex solution:
But for the sake of both efficiency and maintainability, you're probably better off with a mixed solution like the one you posted.
如果您只想将一个实例更改
为
If you only want one instance change
to