regular-expressions – Regular expression: find all numbers not on the edges of a string

Question:

Hello. Please help me write a regular expression. For instance:

test = '12asiudas8787hajshd986q756tgs87ta7d6-12js01'
test.scan(регулярное_выражение)

The result should be

["8787", "986", "756", "87", "7", "6", "12"]

In other words, a regular expression like / \ d + /, only to ignore numbers on the edges of the line.

Answer:

You can proceed as follows

test = '12asiudas8787hajshd986q756tgs87ta7d6-12js01'
arr = []
test.scan(/(?<=[^\d])(\d+)(?=[^\d])/) do |match|
  arr << match[0]
end
p arr
Scroll to Top