如何匹配字符串中的最后一次出现与正则表达式

时间:2022-11-02 21:45:22

Hi I am working on RoR project with ruby-2.5.0 and Rails 5. I have a pattern to find time from a string as follows:-

嗨我正在使用ruby-2.5.0和Rails 5处理RoR项目。我有一个模式可以从字符串中查找时间如下: -

time_pattern = /(\d{2}\:\d{2}:\d{2}|\d{2}\:\d{2})/
time = mystr[time_pattern]

It retuns the first matching time in the string for example if there are two times are present in the string "14:04" and "14:05" it will always return the first one which is "14:04". i need to find the second one. Please help me to find the second match using regex. I have also tried the scan method like:-

它返回字符串中的第一个匹配时间,例如,如果字符串“14:04”和“14:05”中存在两次,则它将始终返回第一个“14:04”。我需要找到第二个。请帮我找到使用正则表达式的第二场比赛。我也尝试了扫描方法,如: -

time = params.to_s.scan(time_pattern).uniq.flatten.last

But my rubocop throws an error Methods exceeded maximum allowed ABC complexity (1) Please help. Thanks in advance.

但我的rubocop抛出一个错误方法超出了最大允许的ABC复杂度(1)请帮助。提前致谢。

2 个解决方案

#1


1  

Try using the following expression: /(\d{2}:\d{2}(:\d{2})?)/g.

尝试使用以下表达式:/(\ d {2}:\ d {2}(:\ d {2})?)/ g。

Here is an example of it running in ruby:

以下是在ruby中运行的示例:

re = /(\d{2}:\d{2}(:\d{2})?)/
str = ' "14:04" and "14:05"'

# Get matches
matches = str.scan(re)

# Get last match
matches = str.scan(re)

# Get last match
lastMatch = str.scan(re).last[0]

# Print last match
puts lastMatch.to_s
# OUTPUT => "14:05"

# Print the match result
str.scan(re) do |match|
    puts match.to_s
end

#2


0  

Last Regex Match

To get the last match you can simply call #last on the #scan result.

要获得最后一个匹配,您只需在#scan结果上调用#last即可。

regex = /(\d{2}\:\d{2}:\d{2}|\d{2}\:\d{2})/ # original
# or
regex = /\d{2}(?::\d{2}){1,2}/ # kudos @ctwheels

string = '12:01, 12:02, 12:03:04, 12:05'

string.scan(regex).last
#=> "12:05"

ABC-Metric

The ABC Metric is more complex and would require having a look at your whole method. But here is a blog post that explains it pretty well: Understanding Assignment Branch Condition

ABC Metric更复杂,需要查看整个方法。但是这篇博文很好地解释了它:理解作业分支条件

Another options is to change the max ABC size in the RuboCop settings. Have a look at the RuboCop Configuration Documentation and the default configuration (Metrics/AbcSize max size is set to 15 by default).

另一个选项是更改RuboCop设置中的最大ABC大小。查看RuboCop配置文档和默认配置(Metrics / AbcSize max size默认设置为15)。

#1


1  

Try using the following expression: /(\d{2}:\d{2}(:\d{2})?)/g.

尝试使用以下表达式:/(\ d {2}:\ d {2}(:\ d {2})?)/ g。

Here is an example of it running in ruby:

以下是在ruby中运行的示例:

re = /(\d{2}:\d{2}(:\d{2})?)/
str = ' "14:04" and "14:05"'

# Get matches
matches = str.scan(re)

# Get last match
matches = str.scan(re)

# Get last match
lastMatch = str.scan(re).last[0]

# Print last match
puts lastMatch.to_s
# OUTPUT => "14:05"

# Print the match result
str.scan(re) do |match|
    puts match.to_s
end

#2


0  

Last Regex Match

To get the last match you can simply call #last on the #scan result.

要获得最后一个匹配,您只需在#scan结果上调用#last即可。

regex = /(\d{2}\:\d{2}:\d{2}|\d{2}\:\d{2})/ # original
# or
regex = /\d{2}(?::\d{2}){1,2}/ # kudos @ctwheels

string = '12:01, 12:02, 12:03:04, 12:05'

string.scan(regex).last
#=> "12:05"

ABC-Metric

The ABC Metric is more complex and would require having a look at your whole method. But here is a blog post that explains it pretty well: Understanding Assignment Branch Condition

ABC Metric更复杂,需要查看整个方法。但是这篇博文很好地解释了它:理解作业分支条件

Another options is to change the max ABC size in the RuboCop settings. Have a look at the RuboCop Configuration Documentation and the default configuration (Metrics/AbcSize max size is set to 15 by default).

另一个选项是更改RuboCop设置中的最大ABC大小。查看RuboCop配置文档和默认配置(Metrics / AbcSize max size默认设置为15)。