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 ...

Get Text Processing with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.