# Help with regex

**URL:** <https://www.ruby-forum.com/t/help-with-regex/120946>\
**Category:** Ruby\
**Created:** [November 22, 2007, 12:50am UTC](https://www.ruby-forum.com/t/help-with-regex/120946 "2007-11-22T00:50:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 22, 2007, 12:50am UTC](https://www.ruby-forum.com/t/help-with-regex/120946/1 "2007-11-22T00:50:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 22, 2007, 12:55am UTC](https://www.ruby-forum.com/t/help-with-regex/120946/2 "2007-11-22T00:55:36Z")

</div>

On Nov 21, 5:46 pm, Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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:

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 22, 2007, 12:57am UTC](https://www.ruby-forum.com/t/help-with-regex/120946/3 "2007-11-22T00:57:21Z")

</div>

Junkone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> 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.

---

<div class="post-metadata">

**Author:** ![Junkone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e274bd/32.png) [@Junkone](https://www.ruby-forum.com/u/Junkone)\
**Post date:** [November 22, 2007, 3:14am UTC](https://www.ruby-forum.com/t/help-with-regex/120946/4 "2007-11-22T03:14:42Z")

</div>

# From: Junkone [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# a=“USD.CHF.ABC”

# irb(main):024:0\> a.match(/^USD/)

# =\> #MatchData:0x2e6be30

# irb(main):025:0\> $1

# =\> nil

$1 want captures, try

a  
#=\> “USD.CHF.ABC”  
a =~ /^USD/  
#=\> 0  
$1  
#=\> nil  
$&  
#=\> “USD”  
$~[0]  
#=\> “USD”

kind regards -botp
