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?
···
--
Posted via http://www.ruby-forum.com/.
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?
--
Posted via http://www.ruby-forum.com/.
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)
s = 'hello_world_123'
=> "hello_world"123"
m = s.match /\w+$/
=> #<MatchData "hello_world_123">
m[0]
=> "hello_world_123"
However, assuming you want to explicitly match /_[0-9]+/ at the end
separately, you could use:
m = s.match /\w+?(_\d+)?$/
=> #<MatchData "hello_world_123" 1:"_123">
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:
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.)
--
Posted via http://www.ruby-forum.com/\.
This is a splendid answer, thanks for all the insights.
--
Posted via http://www.ruby-forum.com/.
It depends what you mean by combine. If you want to match either then
you could use the pipe "|" character as an "or" operator.
--
Posted via http://www.ruby-forum.com/.
Ken Paul wrote in post #1101887:
This is a splendid answer, thanks for all the insights.
No worries.
--
Posted via http://www.ruby-forum.com/\.
Ken Paul wrote in post #1101887:
This is a splendid answer, thanks for all the insights.
Yes @ Matthew Kerwin is awesome to deliver the things from the very
basic. I like this one. ![]()
--
Posted via http://www.ruby-forum.com/\.
And if you have to work with the original regexps, you can use the
method Regexp#union:
Jesus.
On Sat, Mar 16, 2013 at 11:08 AM, Joel Pearson <lists@ruby-forum.com> 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.