.net 从字符串中提取数字的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4187356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regular expression to extract numbers from a string
提问by Martin Robins
Can somebody help me construct this regular expression please...
有人可以帮我构造这个正则表达式吗...
Given the following strings...
鉴于以下字符串...
- "April ( 123 widgets less 456 sprockets )"
- "May (789 widgets less 012 sprockets)"
- “四月(123 个小部件减去 456 个链轮)”
- “五月(789 个小部件减去 012 个链轮)”
I need a regular expression that will extract the two numbers from the text. The month name will vary. The brackets, "widgets less" and "sprockets" text is not expected to change between strings, however it would be really useful if this text was able to be varied as well.
我需要一个正则表达式来从文本中提取两个数字。月份名称会有所不同。括号、“widgets less”和“sprockets”文本预计不会在字符串之间改变,但是如果这个文本也能够变化,它会非常有用。
Thanks in advance.
提前致谢。
回答by Seattle Leonard
if you know for sure that there are only going to be 2 places where you have a list of digits in your string and that is the only thing you are going to pull out then you should be able to simply use
如果您确定只有 2 个地方在您的字符串中有一个数字列表,并且这是您要提取的唯一内容,那么您应该能够简单地使用
\d+
回答by Tim Pietzcker
^\s*(\w+)\s*\(\s*(\d+)\D+(\d+)\D+\)\s*$
should work. After the match, backreference 1 will contain the month, backreference 2 will contain the first number and backreference 3 the second number.
应该管用。匹配后,反向引用 1 将包含月份,反向引用 2 将包含第一个数字,反向引用 3 将包含第二个数字。
Explanation:
解释:
^ # start of string
\s* # optional whitespace
(\w+) # one or more alphanumeric characters, capture the match
\s* # optional whitespace
\( # a (
\s* # optional whitespace
(\d+) # a number, capture the match
\D+ # one or more non-digits
(\d+) # a number, capture the match
\D+ # one or more non-digits
\) # a )
\s* # optional whitespace
$ # end of string
回答by FrustratedWithFormsDesigner
you could use something like:
你可以使用类似的东西:
[^0-9]+([0-9]+)[^0-9]+([0-9]+).+
[^0-9]+([0-9]+)[^0-9]+([0-9]+).+
Then get the first and second capture groups.
然后得到第一个和第二个捕获组。

