0

Given text with several numbers such as

showing 1 - 20 of 25 records

how could I use a regex to extract all the numbers? I would like an expression we could easily use with 'n' numbers.

I can simply use

\d

to identify the three numbers, but then I am stuck on how to pick each.

So far, I have

 \b\d\b 

for the first, and

(\d+)(?!.*\d)

for the last, but I can't figure out the middle.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • `input_str.scan(/(\b\d+\b)/).each do|m|` – rock321987 Apr 08 '16 at 02:34
  • Your example has all positive integers, but you didn't state whether negative numbers (or floats for that matter) may be present. If the string may include negative integers, extracting them requires additional rules. Consider, for example, "showing 1-20 of 25 records". – Cary Swoveland Apr 08 '16 at 03:43
  • See http://stackoverflow.com/questions/80357/match-all-occurrences-of-a-regex – sschmeck Apr 08 '16 at 06:57

1 Answers1

1

Just do:

"showing 1 - 20 of 25 records".scan(/\d+/)

The start and the end of each match will automatically be a number border.

sawa
  • 165,429
  • 45
  • 277
  • 381