Matching

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

/[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

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

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

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

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
[email protected]

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
[email protected]

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
[2] Programming Ruby: The Pragmatic Programmer's Guide

Rob B. http://agileconsultingllc.com
[email protected]

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
[email protected]

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