如何在正则表达式匹配中增加? [英] How do I do a increment in a regex match?
问题描述
如何添加一个选项以将正则表达式的数字部分增加+1。
我写了以下示例
我的尝试:
string input =dsfjl kfjd dsfds fds sd abc3,abc4,abc5 dsfdgfdf ggd gdfg dfg gdfg dfg gdf gf g abc13 abc14 abc20 abc21 abc22 dsfsdf abc13,abc4,abc5;
正则表达式rx =新正则表达式(@(?:abc(\d +)\ * *,?\ * *){2,} abc(\d +));
foreach(在Regex.Matches中匹配m(输入,rx.ToString())){
Console.WriteLine(m.Value);
}
Console.ReadKey();
生成的输出是
abc3,abc4,abc5
abc13 abc14 abc20 abc21 abc22
abc13,abc4,abc5
但我想得到输出为
abc3,abc4,abc5
abc20 abc21 abc22
即只获得数字增加+1的连续匹配
如何在我的代码中实现?
< blockquote>不要。
正则表达式是字符串处理器,并且在模式匹配方面非常出色 - 但它们没有任何数字概念,所以递增计数器的整个想法不具备对他们来说很容易 - 你必须手动编码每个数字并处理溢出,记住也要处理每个数字。
使用正则表达式捕获匹配,但处理匹配在你的代码中建立排序并删除那些不合适的。
Quote:如何在正则表达式匹配中增加?
你没有
使用RegEx获取所有匹配的列表,然后使用一小段代码来过滤/订购列表。
*****
只是一些有趣的链接,可以帮助构建和调试RegEx。
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
< a href =http://regexstorm.net/tester> .NET正则表达式测试程序 - 正则表达式风暴 [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,&测试RegEx [ ^ ]
此节目RegEx是一个很好的图表,它非常有助于理解RegEx的作用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
How do I add an option to increment the digit part of a regex by +1.
I have written the below example
What I have tried:
string input="dsfjl kfjd dsfds fds sd abc3, abc4, abc5 dsfdgfdf ggd gdfg dfg gdfg dfg gdf gf g abc13 abc14 abc20 abc21 abc22 dsfsdf abc13, abc4, abc5"; Regex rx = new Regex(@"(?:abc(\d+)\s*,?\s*){2,}abc(\d+)"); foreach (Match m in Regex.Matches(input,rx.ToString())) { Console.WriteLine(m.Value); } Console.ReadKey();
The output generated is
abc3, abc4, abc5
abc13 abc14 abc20 abc21 abc22
abc13, abc4, abc5
But I want to get the output as
abc3, abc4, abc5
abc20 abc21 abc22
i.e. get only the consecutive matches that have the digits incremented by +1
How do I implement that in my code?
Don't.
Regular expressions are string processors, and are excellent at pattern matching - but they don't have any concept of numbers, so the whole idea of "incrementing a counter" does not come at all easy to them - you would have to manually code for each digit and handle overflow, remembering to handle each digit there as well.
Use a regex to capture the the matches, but process the matches in your code to establish the ordering and remove those that don't fit.
Quote:How do I do a increment in a regex match?
You don't
Use the RegEx to get a list of all matches, then use a little piece of code to filter/order the list as you want.
*****
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
这篇关于如何在正则表达式匹配中增加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!