Aria Aria - 11 days ago 5x
Python Question

Separate street name from street number

I am trying to separate street names from street numbers which have these patterns:

  1. "street 12" --- name:street , number:12

  2. "street12" --- name:street , number:12

  3. "street 12a" --- name:street , number:12a

  4. "street12a" --- name:street , number:12a

What is the regex to get the street name, and the regex to get the street number in php and python?

Note: The number is always after the street name so I guess that should shorten it.


b01 b01

Try this as see if it works for you:

$subjects = array( "street 12", "street12", "street 12a", "street12a" );
foreach( $subjects as $subject )
    if ( preg_match('/([^\d]+)\s?(.+)/i', $subject, $result) )
       var_dump( $result );
die_r( $result  );

The only part you need is this:

// Find a match and store it in $result.
if ( preg_match('/([^\d]+)\s?(.+)/i', $subject, $result) )
    // $result[1] will have the steet name
    $streetName = $result[1];
    // and $result[2] is the number part. 
    $streetNumber = $result[2];