Combine two regular expressions

Hi there,

How to combine the two regular expressions into one?

reg1 = /\w+[_][0-9]+$/
reg2 = /\w+$/

it seems that match(/\w+([_][0-9]+)?$/ does not work, any idea?

This is a splendid answer, thanks for all the insights.

Ken P. wrote in post #1101884:

Hi there,

How to combine the two regular expressions into one?

reg1 = /\w+[_][0-9]+$/
reg2 = /\w+$/

it seems that match(/\w+([_][0-9]+)?$/ does not work, any idea?

A couple of things:

  • ( and ) match literal ‘(’ and ‘)’ characters. In Ruby regex you
    use unescaped parentheses to mark subpatterns/groups, thus: /a(bc)d/

So: /\w+([_][0-9]+)?$/

Also:

  • [] doesn’t need to be in a character class, /[]/ matches the same
    as /_/
  • there’s a special regex metacharacter \d that matches exactly [0-9]

So you could use:
reg1 = /\w+\d+$/
reg2 = /\w+$/
regX = /\w+(
\d+)?$/

Although that already looks a bit less readable than your way…

Also:

  • \w matches underscores and digits as well as letters, so /\w+/ already
    matches /_[0-9]+/ (in other words, reg2 will already match everything
    you intended from the combined pattern)

irb> s = ‘hello_world_123’
=> “hello_world"123”
irb> m = s.match /\w+$/
=> #<MatchData “hello_world_123”>
irb> m[0]
=> “hello_world_123”

However, assuming you want to explicitly match /_[0-9]+/ at the end
separately, you could use:

irb> m = s.match /\w+?(_\d+)?$/
=> #<MatchData “hello_world_123” 1:“_123”>
irb> m[1]
=> “_123”

Note the question mark after \w+? , it makes it un-greedy (i.e. it
doesn’t gobble up the entire rest of the string, the way \w+ would).

Or assuming you actually want to match LETTER+ [ “_” NUMBER+ ] you could
use:

irb> m = s.match /[A-Za-z]+(_\d+)?$/
=> #<MatchData “world_123” 1:“_123”>

Note how it didn’t match “hello_” since that contains non-letters. Or
various other combinations and permutations.

All this gleaned from Class: Regexp (Ruby 1.9.3) (as
well as a couple of decades playing with regular expressions.)

Ken P. wrote in post #1101887:

This is a splendid answer, thanks for all the insights.

Yes @ Matthew K. is awesome to deliver the things from the very
basic. I like this one. :slight_smile:

Ken P. wrote in post #1101887:

This is a splendid answer, thanks for all the insights.

No worries.

It depends what you mean by combine. If you want to match either then
you could use the pipe “|” character as an “or” operator.

On Sat, Mar 16, 2013 at 11:08 AM, Joel P. [email protected]
wrote:

It depends what you mean by combine. If you want to match either then
you could use the pipe “|” character as an “or” operator.

And if you have to work with the original regexps, you can use the
method Regexp#union:

Jesus.