While you can use a pattern, the problem of splitting this into its parts can also be easily done using other Ruby methods:
Using split
:
s = "Josua de Grave* (1643-1712)"
name, dates = s.split('*') # => ["Josua de Grave", " (1643-1712)"]
birth, death = dates[2..-2].split('-') # => ["1643", "1712"]
Or, using scan
:
*name, birth, death = s.scan(/[[:alnum:]]+/) # => ["Josua", "de", "Grave", "1643", "1712"]
name.join(' ') # => "Josua de Grave"
birth # => "1643"
death # => "1712"
If I was using a pattern, I'd use this:
name, birth, death = /^([^*]+).+?(\d+)-(\d+)/.match(s)[1..3] # => ["Josua de Grave", "1643", "1712"]
name # => "Josua de Grave"
birth # => "1643"
death # => "1712"
/(^[^*]+).+?(\d+)-(\d+)/
means:
^
start at the beginning of the buffer
([^*]+)
capture everything not *
, where it'll stop capturing
.+?
skip the minimum until...
(\d+)
the year is matched and captured
-
match but don't capture
(\d+)
the year is matched and captured
Regexper helps explain it as does Rubular.