Forum: Ruby Matching

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Shandy N. (Guest)
on 2008-11-13 00:41
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
Peter S. (Guest)
on 2008-11-13 00:50
(Received via mailing list)
>
> /[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
Peter S. (Guest)
on 2008-11-13 01:00
(Received via mailing list)
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
Shandy N. (Guest)
on 2008-11-13 16:40
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
Shandy N. (Guest)
on 2008-11-14 00:52
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
Rob B. (Guest)
on 2008-11-14 01:35
(Received via mailing list)
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
removed_email_address@domain.invalid
Rob B. (Guest)
on 2008-11-14 02:19
(Received via mailing list)
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
removed_email_address@domain.invalid
Shandy N. (Guest)
on 2008-11-14 18:04
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
> 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
Rob B. (Guest)
on 2008-11-14 18:49
(Received via mailing list)
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] http://www.pragprog.com/titles/ruby/programming-ruby
[2] http://www.ruby-doc.org/docs/ProgrammingRuby/

Rob B.    http://agileconsultingllc.com
removed_email_address@domain.invalid
This topic is locked and can not be replied to.