从字符串中提取数字的正则表达式

时间:2021-12-29 21:13:51

Can somebody help me construct this regular expression please...

有人能帮我构造这个正则表达式吗?

Given the following strings...

鉴于以下字符串…

  • "April ( 123 widgets less 456 sprockets )"
  • “四月(123 widget less 456 spro佝偻病)”
  • "May (789 widgets less 012 sprockets)"
  • "May (789 widget less 012 spro佝偻病)"

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.

我需要一个正则表达式来从文本中提取这两个数字。月份的名称会有所不同。括号“小部件”和“spro佝偻病”文本预计不会在字符串之间发生变化,但是如果这个文本也能够变化,那么它将非常有用。

Thanks in advance.

提前谢谢。

3 个解决方案

#1


28  

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

如果你确定只有两个地方你的字符串中有一个数字列表这是你唯一要取出的东西那么你应该可以简单地使用它

\d+

#2


23  

^\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.

应该工作。比赛结束后,backreference 1将包含这个月,backreference 2将包含第一个数字,然后返回第二个数字。

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

#3


2  

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.

然后获取第一个和第二个捕获组。

#1


28  

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

如果你确定只有两个地方你的字符串中有一个数字列表这是你唯一要取出的东西那么你应该可以简单地使用它

\d+

#2


23  

^\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.

应该工作。比赛结束后,backreference 1将包含这个月,backreference 2将包含第一个数字,然后返回第二个数字。

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

#3


2  

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.

然后获取第一个和第二个捕获组。