正则表达式匹配Ruby中已知字符串后的1-3位数

时间:2022-07-26 19:29:26

I need to match the numbers in the following strings. It is possible they may be part of longer strings with other numbers in them, so I specifically want to match the number(s) occuring directly after the space which follows the text 'Error Code':

我需要匹配以下字符串中的数字。它们可能是较长字符串的一部分,其中包含其他数字,因此我特别希望匹配在“错误代码”文本后面的空格后面直接出现的数字:

Error Code 0  # Match = 0
Error Code 45 # Match = 45
Error Code 190 # Match = 190

Also possible:

也可能:

Some Words 12 Error Code 67 Some Words 77 # Match = 67

I'm using someString.match(regEx)[0] but I can't get the regex right.

我正在使用someString.match(regEx)[0],但我无法正确使用正则表达式。

4 个解决方案

#1


17  

/(?:Error Code )[0-9]+/

This uses a non-capturing group (not available in all regex implementations.) It will say, hey the String better have this phrase, but I don't want this phrase to be part of my match, just the numbers that follow.

这使用非捕获组(在所有正则表达式实现中都不可用。)它会说,嘿String更好地拥有这个短语,但我不希望这个短语成为我的匹配的一部分,只是后面的数字。

If you only want between 1 and three digits matched:

如果您只想匹配1到3位数:

/(?:Error Code )[0-9]{1,3}/

With Ruby you should run into very few limitations with your regex. Aside from conditionals, there isn't very much Ruby's regex cannot do.

使用Ruby,你的正则表达式应该受到很少限制。除了条件之外,没有太多Ruby的正则表达式无法做到。

#2


4  

Given the string "Error Code 190", this will return "190" and not "Error Code" like the other regex example provided here.

给定字符串“Error Code 190”,这将返回“190”而不是“Error Code”,就像这里提供的另一个正则表达式示例一样。

(?<=Error Code.*)[0-9]

#3


4  

I'd use the following regex:

我使用以下正则表达式:

/Error\sCode\s\d{1,3}/

Or, with named groups:

或者,使用命名组:

/Error\sCode\s(?<error_code>\d{1,3})/

The last one will capture the error code, the number, under a named group. Note that \s matchs whitespaces but it's unnecessary unless the x flag is specified. You can access match captures like this:

最后一个将捕获命名组下的错误代码,即数字。请注意\ s匹配空格,但除非指定了x标志,否则它是不必要的。您可以像这样访问匹配捕获:

str = "Error Code 0\nError Code 45\nError Code 190"
error_codes = str.lines.map{|l|l.match(regex)[:error_code]}
#=> ["0", "45", "190"]

#4


0  

You can use this regexp: Only digits, count from 1 to 3:

您可以使用此正则表达式:仅数字,从1到3计数:

regexp = "^\\d{1,3}$"

#1


17  

/(?:Error Code )[0-9]+/

This uses a non-capturing group (not available in all regex implementations.) It will say, hey the String better have this phrase, but I don't want this phrase to be part of my match, just the numbers that follow.

这使用非捕获组(在所有正则表达式实现中都不可用。)它会说,嘿String更好地拥有这个短语,但我不希望这个短语成为我的匹配的一部分,只是后面的数字。

If you only want between 1 and three digits matched:

如果您只想匹配1到3位数:

/(?:Error Code )[0-9]{1,3}/

With Ruby you should run into very few limitations with your regex. Aside from conditionals, there isn't very much Ruby's regex cannot do.

使用Ruby,你的正则表达式应该受到很少限制。除了条件之外,没有太多Ruby的正则表达式无法做到。

#2


4  

Given the string "Error Code 190", this will return "190" and not "Error Code" like the other regex example provided here.

给定字符串“Error Code 190”,这将返回“190”而不是“Error Code”,就像这里提供的另一个正则表达式示例一样。

(?<=Error Code.*)[0-9]

#3


4  

I'd use the following regex:

我使用以下正则表达式:

/Error\sCode\s\d{1,3}/

Or, with named groups:

或者,使用命名组:

/Error\sCode\s(?<error_code>\d{1,3})/

The last one will capture the error code, the number, under a named group. Note that \s matchs whitespaces but it's unnecessary unless the x flag is specified. You can access match captures like this:

最后一个将捕获命名组下的错误代码,即数字。请注意\ s匹配空格,但除非指定了x标志,否则它是不必要的。您可以像这样访问匹配捕获:

str = "Error Code 0\nError Code 45\nError Code 190"
error_codes = str.lines.map{|l|l.match(regex)[:error_code]}
#=> ["0", "45", "190"]

#4


0  

You can use this regexp: Only digits, count from 1 to 3:

您可以使用此正则表达式:仅数字,从1到3计数:

regexp = "^\\d{1,3}$"