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

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Question Regular Expessions


    Without using the split function and using only regular expression can any one please tell me how do i extract
    Dearborn and MI from the below line
    Norma Corder:3978572735:74 Pine Street, Dearborn, MI 23874:3/28/45:245700
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    /,(.+?), (.{2})/
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    11
    Rep Power
    0

    Smile


    thanks for the expr. but i tried the first grouping with (.+) which worked fine.

    Any reason why (.+?) was used??


    thnx
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,940
    Rep Power
    1225
    It's true that the non-greedy ? quantifier wasn't needed in your posted data, but I added it just in-case your actual data has or will have extra csv fields.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    836
    Rep Power
    496
    Originally Posted by csvraju1985
    thanks for the expr. but i tried the first grouping with (.+) which worked fine.

    Any reason why (.+?) was used??
    (.+) or (.*) is very often a very dangerous regular expression, unless you know damn well what you are doing, as it will often match much more that you think.

    Suppose you want to capture the first bold world in a line like this:

    "The quick brown <b>fox</b> jumps over the lazy <b>dog</b>"

    If you use a regular expression like /<b>(.+)<\/b>/, you will not capture "fox" as you may wish, but you will capture "fox</b> jumps over the lazy <b>dog", which is probably not what you want, because the '+' is a greedy quantifier that will match as much as it can until the final </b>. Using instead /<b>(.+?)<\/b>/ will give you a non greedy quantifier and you will capture only "fox".

IMN logo majestic logo threadwatch logo seochat tools logo