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

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Question Match multiple versions of city names?


    i need a regex to match the following possible variations of city name patterns:

    city
    city st. town
    st. city
    big city
    twin-city
    some-town-city

    the `st.` can be literal if necessary. case insensitive.

    this is what i have so far, though some of it was built by RegexBuddy and i have no idea what the ?: means...
    Code:
    [a-z]+(?:[\s]?)(?:[\.]?[\s]?)?[a-z]*
    thanks gang!

    WR!
  2. #2
  3. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Except for alternation (by writing a regex that means "this or this or this"), only the first three can be combined together. The others are about matching completely different strings.
    Code:
    (st. city|city( st. town)?)
    (?:...) means that the subpattern isn't "remembered" for later. Check our resources sticky for more information.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0
    ah! thank you very much! i didn't think i could do it in one pattern, so thanks for verifying that. dang, eh?

    i've done some reading since last night and was wondering if this was something i could do with word boundaries...?

    bah. in the end i suppose i could just test against anything NOT alphabetic or dot/dash...

    thank you so much.

    WR!
  6. #4
  7. Jealous Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,302
    Rep Power
    9400
    Word boundaries are the difference between "ark" and "market".

    What kind of text are you dealing with? Do you know specifically which cities/states/etc. you're searching for?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0
    i'm just trying to validate city names and make sure they don't input garbage. the dot, dash and space are the only allowed chars in city names, so i thought i'd test for those, but dump anything else. since we're generating legal docs, i thought it would be a nice feature to make sure they don't live in 123land or some crap like that.

    i suppose, at the end of the day, if the client is that stupid, then we'll take their money and be done with it... :P

    hope that helped.

    WR!
  10. #6
  11. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    94
    Hi Whiterau,

    requinix is quite right that we need alternation on this one.

    Here is a pattern that matches the sample text you gave. It groups options #3, 4 and 5 on one line of the alternation. The other options have their own line.

    Just dump this in the pattern window of regexbuddy:
    Code:
    (?ix)
    ^[A-Z]+ # take the first word
    (?:$| # just city
    (?:-|[.]?[ ])[A-Z]+$| # st. city, big city or twin city
    [ ][A-Z]+\.[ ][A-Z]+$|  # city st. town
    (?:-[A-Z]+){2}$   # some-town-city
    )
    Then dump this in the test string window:
    Code:
    city
    city st. town
    st. city
    big city
    twin-city
    some-town-city
    Make sure that RB is set to line-by-line.
    To see what each line does, remove the "Z": some of the strings will be unmatched.

    Please let me know if this is what you are looking for.

    Wishing you both a fun weekend and a fruitful new year.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Lightbulb


    absolutely brilliant. thank you so much. there's no way i would have solved this one on my own. this padawan has much to learn in the ways of RegEx. lol.

    what is the (?ix) actually do? i see that it is a mode modifier but JavaScript does not support those... any way around that you know of? is it looking for a line-break? there aren't any. the list is just possible entry types. they will be evaluated individually as the client enters the name in the form.

    thank you so much.

    WR!
  14. #8
  15. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    94
    Hi WhiteRau, thrilled this is working for you.

    what is the (?ix) actually do?
    The x is for the "comment mode" or "whitespace mode" that enabled me to write the regex on multiple lines (easier to read). If you remove the comments (everything after the #) you can bring it back to one line. The i is for "case insensitive". In javascript you can use the /i modifier instead, so you can get rid of (?ix) and get everything on one line.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Posts
    5
    Rep Power
    0

    Talking


    thank you so much. if you don't mind me asking, how long have you been doing RegEx? everytime i think i have a grip on it, it explodes... having RegExBuddy is helping a LOT.

    may i ask what resources you'd recommend for learning more? the O'Reilly book is in my sights, but is there anything else?

    thanks for your time!

    WR!
  18. #10
  19. Turn left at the third duck
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2011
    Location
    Nelson, NZ
    Posts
    112
    Rep Power
    94
    Hi again WhiteRau!

    may i ask what resources you'd recommend for learning more?
    The same question came up on another thread a few days ago, so instead of doing a half job of repeating myself, I thought I would write a comprehensive answer to which I could refer time and again. So here is my detailed answer on the Regex Resources thread.

    The learning curve is steep, but that's a good thing. If you apply yourself, you can know as much as I do in about a month!


    Wishing you a beautiful weekend,

IMN logo majestic logo threadwatch logo seochat tools logo