Regex Problem

Hello all,

I am trying to match this pattern: >1_9_1912_F3

Here is my stab at it:
if f3_entry =~ /(>__*_F3)/
f3_out.puts f3_entry
end

Obviously, it doesn’t work. I need to match whatever is between those
underscores. Am i going in the right direction? Thanks in advance!

  • C

Check out rubular.com where you can interactively match text with your
regular expression. This will help you confirm when you’re on the right
track.

From my iPhone

Pat

I am trying to match this pattern: >1_9_1912_F3

Here is my stab at it:
if f3_entry =~ /(>__*_F3)/
f3_out.puts f3_entry
end

Obviously, it doesn’t work. I need to match whatever is between those
underscores. Am i going in the right direction? Thanks in advance!

If that’s all you need then I’d just use split and call it a day:
irb(main):001:0> “1_9_1912_F3”.split(/_/)=> [“1”, “9”, “1912”, “F3”]
Regards,Chris W.Twitter: http://www.twitter.com/cwgem

On Sep 9, 2011, at 9:25 AM, “Cyril J.” [email protected]
wrote:

Thanks Pat rubular helped a lot.
I used :

([0-9][_][0-9][][0-9]*[])

Equivalent: /(\d*\d*\d*_)/

Or even: /((?:\d*_){3})/

On Fri, Sep 9, 2011 at 9:39 PM, Gavin K. [email protected] wrote:

On Sep 9, 2011, at 9:25 AM, “Cyril J.” [email protected] wrote:

Thanks Pat rubular helped a lot.
I used :

([0-9][_][0-9][][0-9]*[])

Equivalent: /(\d*\d*\d*_)/

Or even: /((?:\d*_){3})/

Cyril, are you sure you want to also match “___”? If not, use

/(?:\d+_){3}F3/

Kind regards

robert

El 09/09/2011 17:26, “Cyril J.” [email protected] escribi:

Thanks Pat rubular helped a lot.
I used :

([0-9][_][0-9][][0-9]*[])

I probably wasn’t too clear with what I wanted earlier, so I apologize
for that. Thanks for the help.

[0-9] is the same as \d:

and * means 0 or more, which I’m not sure it’s really what you need. If
you
don’t want to check for a specific number of digits but need 1 or more
use +

/>\d+\d+\d+_F3/

If you want to limit to specific number of digits, you can use {a} to
specify a range of numbers (or range of numbers), or no quantifier it
it’s
exactly one occurence. For example:

/>\d_\d_\d{4}_F3/

which means, match >, then a digit, then _, then another digit, then
another
_, then 4 digits, then _F3

Jesus.

Thanks Pat rubular helped a lot.
I used :

([0-9][_][0-9][][0-9]*[])

I probably wasn’t too clear with what I wanted earlier, so I apologize
for that. Thanks for the help.