#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Pulling my hair out..what little I have left! :)


    Hi all- Newbie here.

    I have a string as follows (for example): "I have visited the U.S., Puerto Rico, Canada, U.S. Virgin Islands and the Carribean."

    I want to extract just the country names from that sentence, so: 1) U.S. 2) Puerto Rico 3) Canada 4) U.S. Virgin Islands and 5) Carribean.

    I have something like: (United States|U\.S\.|Canada|Puerto Rico|U\.S\. Virgin Islands|Carribean)(,|\s|and|the)+

    This matches the following: "U.S., Puerto Rico, Canada, U.S. Virgin Islands and the Carribean."

    The problem I have is it matches U.S. twice, where it should only match once (it extracts U.S. from "U.S. Virgin Islands").

    There must be a nicer way of doing this. It sucks to be a newb!

    Thanks for any help.
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,986
    Rep Power
    9397
    *cough* The Carribean isn't a country

    Can't you just ignore the extra match?
    Code:
    (United States|U\.S\.(?! Virgin Islands)|Canada|Puerto Rico|U\.S\. Virgin Islands|Carribean)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0
    Originally Posted by requinix
    *cough* The Carribean isn't a country

    Can't you just ignore the extra match?
    Code:
    (United States|U\.S\.(?! Virgin Islands)|Canada|Puerto Rico|U\.S\. Virgin Islands|Carribean)
    Thanks...I knew there was a simple way of doing this...I was over complicating it!! I'm probably going to have lots more questions to post on this board, hopefully ones that aren't so easy.

    P.S. Anyone recommend a good regex editor? I installed a trial version of regex buddy on Vista and after the first use, it got corrupted! Any freeware out there?
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3886
    For matches such as these, if some of your alternatives are extensions of other (i.e. U.S. Virgin Islands contains U.S. and then some more text) you always need to put the longer one first, so it will attempt to match that one first.

IMN logo majestic logo threadwatch logo seochat tools logo