April 12th, 2010, 01:14 AM
-
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...
April 12th, 2010, 01:57 AM
-
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 !!! 
April 12th, 2010, 02:13 AM
-
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..
April 12th, 2010, 04:01 AM
-
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 !!! 
April 12th, 2010, 09:22 PM
-
i tested it and it works, but the letter and dash in "123 - A" should also be included...
thanks for your time...
April 12th, 2010, 10:38 PM
-
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
I will try my best !!! 
April 13th, 2010, 10:17 PM
-
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*)
April 14th, 2010, 01:56 AM
-
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 !!! 