O'Reilly logo

Text Processing with Ruby by Rob Miller

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Extracting Multiple Matches

You’ve seen how we can use match to check whether a string matches a pattern and to extract capture groups within that pattern. Let’s take a look now at how we can use a pattern to extract many values from within a piece of text, a common requirement of text processing tasks. This will enable us to define a pattern once and then match arbitrary occurrences of it.

In these examples we’re going to work with the following block of text:

 
Here are the top ten countries by population, as of 2013 when the
 
world population was 7 billion.
 
 
China: 1,361,540,000
 
India: 1,237,510,000
 
United States: 317,234,000
 
Indonesia: 237,641,326
 
Brazil: 201,032,714
 
Pakistan: 185,028,000
 
Nigeria: 173,615,000
 
Bangladesh: 152,518,015 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required