[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

help with regex

Junkone

11/21/2007 11:46:00 PM

i have this string
a="USD.CHF.ABC"
irb(main):024:0> a.match(/^USD/)
=> #<MatchData:0x2e6be30>
irb(main):025:0> $1
=> nil


i want to use regex to see if it matches USD as first 3 characters and
here is my attmept. I am not sure why there is no match, any help is
appreciated.
3 Answers

yermej

11/21/2007 11:50:00 PM

0

On Nov 21, 5:46 pm, Junkone <junko...@gmail.com> wrote:
> i have this string
> a="USD.CHF.ABC"
> irb(main):024:0> a.match(/^USD/)
> => #<MatchData:0x2e6be30>
> irb(main):025:0> $1
> => nil
>
> i want to use regex to see if it matches USD as first 3 characters and
> here is my attmept. I am not sure why there is no match, any help is
> appreciated.

You can either assign the MatchData that match returns to a variable:

> m = a.match /^USD/
> m[0]

or you can use parens in your regex to capture the match into $1:

> a.match /^(USD)/
> $1

Carl

11/21/2007 11:53:00 PM

0

Junkone <junkone1@gmail.com> writes:

> i have this string
> a="USD.CHF.ABC"
> irb(main):024:0> a.match(/^USD/)
> => #<MatchData:0x2e6be30>
> irb(main):025:0> $1
> => nil
>
>
> i want to use regex to see if it matches USD as first 3 characters and
> here is my attmept. I am not sure why there is no match, any help is
> appreciated.

Actually, there was a match and a MatchData object was returned (which
you silently ignored).

Maybe this example will help:
IRB-> match_data = "USD.CHF.ABC".match(/^USD/)
=> #<MatchData:0x578744>
IRB-> match_data.to_a
=> ["USD"]
IRB-> match_data = "CHF.ABC.USD".match(/^USD/)
=> nil

Hope that helps.

Peña, Botp

11/22/2007 2:14:00 AM

0

# From: Junkone [mailto:junkone1@gmail.com]=20
# a=3D"USD.CHF.ABC"
# irb(main):024:0> a.match(/^USD/)
# =3D> #<MatchData:0x2e6be30>
# irb(main):025:0> $1
# =3D> nil

$1 want captures, try

a
#=3D> "USD.CHF.ABC"
a =3D~ /^USD/
#=3D> 0
$1
#=3D> nil
$&
#=3D> "USD"
$~[0]
#=3D> "USD"

kind regards -botp