Hello. Please help me create a regular expression. For example:

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 so that numbers at the edges of the string are ignored.

    3 answers 3

    You can do the following:

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

      Positive look-ahead and positive look-behind will help:

       /(?<=\D)\d+(?=\D)/ 
      • In the accepted answer, up to the location of the groups and \D / [^\d] the same. Just letting you know. - D-side
      • All the answers to all everywhere are the same. - Nakilon
       Reg1=/ [[:digit:]]+ [^[:digit:]]+([[:digit:]]+) [^[:digit:]]+([[:digit:]]+) [^[:digit:]]+([[:digit:]]+) [^[:digit:]]+([[:digit:]]+) [^[:digit:]]+([[:digit:]]+) [^[:digit:]]+([[:digit:]]+) [^[:digit:]]+([[:digit:]]+)/x line = '12asiudas8787hajshd986q756tgs87ta7d6-12js01' m = Reg1.match(line) puts m[1..7] 
      • Try to write more detailed answers. I am sure the author of the question will be extremely grateful to you for the expert comment on the code. - Nicolas Chabanovsky