2008/9/26 Thomas B. <tpreal / gmail.com>:
> Chris Ro wrote:
>> place1 = string1.gsub(/.*(\d+)th.*/,'\1')
>
> Hello. I think your approach with using gsub is not the best possible
> here.

Agree.

> It's better to simply find the matching part using match and
> substitute it for the whole string, like this:
> place1 = string1.match(/(\d+)th\b/)[1]

For extraction there is a simpler solution

irb(main):002:0> "He is the 20th."[/(\d+)th\b/, 1]
=> "20"
irb(main):003:0> "25th"[/(\d+)th\b/, 1]
=> "25"

> The \b ensures that the next character after 'th' is not a word
> character (\b is word boundary), and [1] at the end is extracting the
> first bracketed group. It also makes it possible to skip the .* at both
> ends, which is a bit ugly.

Right.

> Apart from that, a useful piece of knowledge about regexps:
> /.*?(\d+)th.*/ will match what you want, because the first .*? will be
> reluctant to eat up more characters, so it will pass to \d+ as many
> digits as it can.

But reluctant is slow (see my benchmark from a few days ago).

Cheer

robert

-- 
use.inject do |as, often| as.you_can - without end