From: Matthew Kerwin Date: 2013-03-16T15:01:51+09:00 Subject: Re: combine two regular expressions Ken Paul 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+$/ => # 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+)?$/ => # 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+)?$/ => # Note how it didn't match "hello_" since that contains non-letters. Or various other combinations and permutations. All this gleaned from http://www.ruby-doc.org/core-1.9.3/Regexp.html (as well as a couple of decades playing with regular expressions.) -- Posted via http://www.ruby-forum.com/.