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

    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0

    Exclamation Matching street no. in regex


    good day.. i would like to ask if you guys know how to match street no. in regex?

    because im helping a friend in his project about regular expression...
  2. #2
  3. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112

    Thumbs up


    Originally Posted by isyan
    good day.. i would like to ask if you guys know how to match street no. in regex?

    because im helping a friend in his project about regular expression...
    For that you need to grep some pattern. How the value is coming in string?
    you have to findout some common pattern of street number..

    for example

    Example 1 : Racecourse Road, Street 1/A, USA
    Example 2 : St.Thomas Road - 2B, USA
    Example 3 : 4, Parssippany Road, Near xyz hotel

    So here are too many different pattern of Street number..
    First do you have same pattern of string for street number?
    or you have different different patterns?

    If it is same then it's easy to made function otherwise need to apply some logic.
    I will try my best !!!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0
    hmmm...

    - this project will accept international street name
    - i only want the number with the attached letter in a street..
    - for example "123-A INGLES ST."
    - so i would match 123-A
    - but the street number(which is 123-A) can be put anywhere in the street(123-A INGLES ST) ex. "INGLES ST. 123 - A", "INGLES 123 A ST."
    -so can you please provide a logic that will match the street number anywhere it is placed in the street

    thanks for replying quickly..
  6. #4
  7. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112
    Originally Posted by isyan
    hmmm...

    - this project will accept international street name
    - i only want the number with the attached letter in a street..
    - for example "123-A INGLES ST."
    - so i would match 123-A
    - but the street number(which is 123-A) can be put anywhere in the street(123-A INGLES ST) ex. "INGLES ST. 123 - A", "INGLES 123 A ST."
    -so can you please provide a logic that will match the street number anywhere it is placed in the street

    thanks for replying quickly..
    Try this way..

    PHP Code:
    <?php 

    $address_str 
    'INGLES ST. 123 - A';

    if(
    preg_match_all('/(\d+)/',$address_str,$matches))
    {
      print 
    "<pre>";
      
    print_r($matches);
      print 
    "</pre>";
    }
    ?>
    NOTE : in this case it will grep all "numeric" value from string..
    And either it is street number or zipcode.. all numeric will store in $matches.
    I will try my best !!!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0
    i tested it and it works, but the letter and dash in "123 - A" should also be included...

    thanks for your time...
  10. #6
  11. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112

    Thumbs up


    Originally Posted by isyan
    i tested it and it works, but the letter and dash in "123 - A" should also be included...

    thanks for your time...
    Then it should be like this

    PHP Code:

    preg_match_all
    ('/(\d+ - \w+)/',$address_str,$matches

    Comments on this post

    • isyan agrees
    I will try my best !!!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2010
    Posts
    4
    Rep Power
    0
    i've made a modification on the one that you gave.. can you simplify this

    (\d+ -* \w+)|(\d+ * \w+)|(\d+-*\w+)|(\w*)(\d+ -* \w+)|(\d+ * \w+)|(\d+-*\w+)|(\w*)
  14. #8
  15. Web Developer
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2006
    Posts
    480
    Rep Power
    112

    Thumbs up


    Originally Posted by isyan
    i've made a modification on the one that you gave.. can you simplify this

    (\d+ -* \w+)|(\d+ * \w+)|(\d+-*\w+)|(\w*)(\d+ -* \w+)|(\d+ * \w+)|(\d+-*\w+)|(\w*)
    Check below example.. It will handle almost all of your case..
    Try out

    PHP Code:

    <?php 

    $str1 
    'INGLES ST. 123 - A';   // CASE 1 //
    $str2 'INGLES ST. 123  A';  // CASE 2 //
    $str3 'INGLES ST. 123-A';  // CASE 3 //
    $str4 'INGLES ST. 123A';  // CASE 4 //
    $str5 'INGLES ST. 123'// CASE 5 //

    $street_name '';

    if(
    preg_match('/(\d+(\s+|)(-|)(\s+|)\w+)/',$str6,$matches))
        
    $street_name $matches[1];

    echo 
    $street_name;

    ?>
    I will try my best !!!

IMN logo majestic logo threadwatch logo seochat tools logo