Helpful Information
 
 
Category: Regex Programming
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...

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.

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..

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

$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 tested it and it works, but the letter and dash in "123 - A" should also be included...

thanks for your time...

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




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

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*)

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

$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;

?>










privacy (GDPR)