# Matching

**URL:** <https://www.ruby-forum.com/t/matching/153177>\
**Category:** Ruby\
**Created:** [November 12, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/matching/153177 "2008-11-12T23:41:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 12, 2008, 11:41pm UTC](https://www.ruby-forum.com/t/matching/153177/1 "2008-11-12T23:41:11Z")

</div>

I am trying to parse out some characters from a string and I am having  
some trouble because the string may have a space or it may not.  
Basically I have a string that looks like this:

Seattle, WA[SEA] or  
Colorado Springs, CO[COS] or  
Denver, CO[DEN]

What I have so far to match the Seattle and Denver strings looks like  
this:

/[A-Z]{1,1}[a-z]+[,]\s[A-Z]+[{1,1}/

This regular expression is trying to grab everything within the []; in  
the cases above this would be SEA, COS, DEN.

How do I make that same match if the initial string has whitespace  
include, such as the Colorado Springs. I could add a \w, but what if the  
section of string before the “,” has more then one space; what if it has  
three, or four, or ten.

If there is a better way to do this I all ears, regular expressions  
isn’t my cup of tea. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 12, 2008, 11:50pm UTC](https://www.ruby-forum.com/t/matching/153177/2 "2008-11-12T23:50:08Z")

</div>

> /[A-Z]{1,1}[a-z]+[,]\s[A-Z]+[{1,1}/
> 
> This regular expression is trying to grab everything within the []; in  
> the cases above this would be SEA, COS, DEN.

If you want to grab everything in brackets, why do you care about the  
text before them?  
You could do just:

> > “Seattle, WA[SEA]”[/[(.+)]/,1]  
> > =\> “SEA”  
> > “Colorado Springs, CO[COS]”[/[(.+)]/,1]  
> > =\> “COS”

etc.

## Cheers, Peter

[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 13, 2008, 12:00am UTC](https://www.ruby-forum.com/t/matching/153177/3 "2008-11-13T00:00:05Z")

</div>

On 2008.11.12., at 23:46, Peter S. wrote:

> > this:
> > 
> > /[A-Z]{1,1}[a-z]+[,]\s[A-Z]+[{1,1}/
> > 
> > This regular expression is trying to grab everything within the [];  
> > in  
> > the cases above this would be SEA, COS, DEN.

Here is a ‘full’ solution that should cover these cases:

> > “Colorado Springs, CO[COS]”[/(\w|\s)+, [A-Z]{2}[(.+?)]/,2]  
> > =\> “COS”  
> > “Denver, CO[DEN]”[/(\w|\s)+, [A-Z]{2}[(.+?)]/,2]  
> > =\> “DEN”

## Cheers, Peter

[http://www.rubyrailways.com](http://www.rubyrailways.com)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 13, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/matching/153177/4 "2008-11-13T23:52:41Z")

</div>

Peter S. wrote:

> Here is a ‘full’ solution that should cover these cases:
> 
> > > “Colorado Springs, CO[COS]”[/(\w|\s)+, [A-Z]{2}[(.+?)]/,2]  
> > > =\> “COS”
> > 
> > > “Denver, CO[DEN]”[/(\w|\s)+, [A-Z]{2}[(.+?)]/,2]  
> > > =\> “DEN”

I have had to refine my search a little, mostly because getting the last  
four characters turned out not to be a simple thing when using regular  
expressions. I have people entering text via an auto\_complete\_text\_field  
in a rails app; my issues is that on my local machine there parameters  
get passed like this:

Denver, CO[DEN] or Colorado Springs, CO[COS]

However, up on the server the parameters get passed like this:

" Denver, CO[DEN] " or " Colorado Springs, CO[COS] "

and for whatever reason (my guess being because regex is a pain in the  
a#%), simply replacing the spaces dident work and cause massive amounts  
of havoic. So I used the code found up above and all was well in the  
world…

Then a new hick-up was encountered:

Toronto, Canada[YYZ]

As you can imagine the regex wouldn’t ketch this expression. So I tried  
this:

```
params[:airport][:city].gsub(/$\s/, "")
  [/(\w|\s)+, [A-Z]{2}|[A-Z][a-z]{1,}\[(.+?)\]/,2]

```

And al’ bee it caught the Torono, Canada[YYZ]. Now, however, it fails to  
get the Denver, CO[DEN]. I must have a complete lack of understanding  
when it comes to regex because I always thought that the “|” in the  
above statement meant “or”, as in, try and match TWO A-Z OR one A-Z  
FOLLOWED BY one or more a-z. I guess I am drawing a total blank because  
I look at the revised code above and I think it should match all the  
above cases and I cannot figure out for the life of me where it is  
failing. Can someon ehelp, thanks

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 13, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/matching/153177/5 "2008-11-13T15:40:30Z")

</div>

Peter S. wrote:

> If you want to grab everything in brackets, why do you care about the  
> text before them?  
> You could do just:

I realized that after I made this post - that I was over thinking the  
problem way to much. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 14, 2008, 12:35am UTC](https://www.ruby-forum.com/t/matching/153177/6 "2008-11-14T00:35:56Z")

</div>

On Nov 13, 2008, at 5:49 PM, Shandy N. wrote:

> four characters turned out not to be a simple thing when using regular
> 
> As you can imagine the regex wouldn’t ketch this expression. So I  
> above statement meant “or”, as in, try and match TWO A-Z OR one A-Z  
> FOLLOWED BY one or more a-z. I guess I am drawing a total blank  
> because  
> I look at the revised code above and I think it should match all the  
> above cases and I cannot figure out for the life of me where it is  
> failing. Can someon ehelp, thanks
> 
> -S

OK, you’re trying to pull the three-letter airport code from within  
the square brackets at the end of the location.

airports = [“Denver, CO[DEN]”,  
“Colorado Springs, CO[COS]”,  
" Denver, CO[DEN] ",  
" Colorado Springs, CO[COS] ",  
“Toronto, Canada[YYZ]”,  
“Erlanger, KY[CVG]”,  
]

airports.each do |airport|  
location, code = airport.strip.match(/(.\*)[(.{3})]/).captures  
puts “#{code} is in ‘#{location}’”  
end

DEN is in ‘Denver, CO’  
COS is in ‘Colorado Springs, CO’  
DEN is in ‘Denver, CO’  
COS is in ‘Colorado Springs, CO’  
YYZ is in ‘Toronto, Canada’  
CVG is in ‘Erlanger, KY’

Got it?

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 14, 2008, 1:19am UTC](https://www.ruby-forum.com/t/matching/153177/7 "2008-11-14T01:19:47Z")

</div>

On Nov 13, 2008, at 5:49 PM, Shandy N. wrote:

> when it comes to regex because I always thought that the “|” in the  
> above statement meant “or”, as in, try and match TWO A-Z OR one A-Z  
> FOLLOWED BY one or more a-z. I guess I am drawing a total blank  
> because  
> I look at the revised code above and I think it should match all the  
> above cases and I cannot figure out for the life of me where it is  
> failing. Can someon ehelp, thanks
> 
> -S

Oh, I see part of your problem: You need to group the alternation to  
see what you want.  
=\> An UPPER with one or more lowers,  
followed by some characters within  
square brackets  
v---------------------v  
/(\w|\s)+, [A-Z]{2}|[A-Z][a-z]{1,}[(.+?)]/  
^----------------^  
=\> Some word or  
space characters,  
a comma,  
and 2 letters

I think you intended to have:  
v------v  
/(\w|\s)+, (?:[A-Z]{2}|[A-Z][a-z]{1,})[(.+?)]/  
^------------^  
Grouped with (?: ) which is a non-capturing group.

(But my earlier message still holds.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 14, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/matching/153177/8 "2008-11-14T17:49:36Z")

</div>

On Nov 14, 2008, at 11:01 AM, Shandy N. wrote:

> > =\> Some word or  
> > (But my earlier message still holds.)  
> > characters. I have a book on Ruby an it is well writen but a list on  
> > methods broken down to what methods are for which classes would be  
> > helpful? Thanks,
> 
> -S

If you don’t have a copy of “Programming Ruby”[1] aka, the Pickaxe  
book, it’s well worth getting. If you are new to Ruby, you can get  
pretty far with the online first edition[2] which covers Ruby 1.6  
(second edition is 1.8, anticipated 3rd edition is 1.9 and currently a  
beta-book).

You can locally use ri (or get fri, the fastri alternative) to get docs.

ri String  
ri String#match  
ri Regexp#match  
ri MatchData

-Rob

[1] [Pragmatic Bookshelf: By Developers, For Developers](http://www.pragprog.com/titles/ruby/programming-ruby)  
[2] [Programming Ruby: The Pragmatic Programmer's Guide](http://www.ruby-doc.org/docs/ProgrammingRuby/)

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 14, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/matching/153177/9 "2008-11-14T17:04:46Z")

</div>

Rob B. wrote:

> Oh, I see part of your problem: You need to group the alternation to  
> see what you want.  
> =\> An UPPER with one or more lowers,  
> followed by some characters within  
> square brackets  
> v---------------------v  
> /(\w|\s)+, [A-Z]{2}|[A-Z][a-z]{1,}[(.+?)]/  
> ^----------------^  
> =\> Some word or  
> space characters,  
> a comma,  
> and 2 letters
> 
> I think you intended to have:  
> v------v  
> /(\w|\s)+, (?:[A-Z]{2}|[A-Z][a-z]{1,})[(.+?)]/  
> ^------------^  
> Grouped with (?: ) which is a non-capturing group.
> 
> (But my earlier message still holds.)
> 
> -Rob
> 
> Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
> removed\_email\_address@domain.invalid

Can you direct me to some good online resources where some Ruby methods  
ar listed? I didn’t know that there was a method for a string named  
“match” that would return a string and not just chomp of various  
characters. I have a book on Ruby an it is well writen but a list on  
methods broken down to what methods are for which classes would be  
helpful? Thanks,

-S
