Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate street name from street number [closed]

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.

Thanks.

like image 882
Aria Avatar asked Sep 20 '11 16:09

Aria


People also ask

What do you write if you don't have a street number?

Just write into the blanks, in order, the usual way that your addresses written. Don't be concerned about the caption that says "street number" or Town" or "ZIP Code" or whatever. Usually there are enough blanks to enter each of the items of your actual address. If not, put two items in one blank space.

How do I split name and address in Excel?

Here are two ways to do it. Mouse and Keyboard: Click the letter above the column where the address info is, and it'll select the entire column. Keyboard Shortcut: Select any cell from the column that has the address info. Then press and hold Ctrl, and hit Space.


1 Answers

I would suggest that the best way to determine when the number starts is when you hit a digit. Thus, you would use

preg_match('/^([^\d]*[^\d\s]) *(\d.*)$/', $address, $match)

Examples:

'Bubbletown 145' => 'Bubbletown', '145'
'Circlet56a' => 'Circle', '56a'
'Bloomfield Avenue 68' => 'Bloomfield Avenue', '68'
'Quibbit Ave       999a' => 'Quibbit Ave', '999a'
'Singletown551abc' => 'Singletown', '551abc'

It will probably be best for you to consider how you want edge cases to be handled, then write a unit test to test your own Regex function.

like image 185
Robert Martin Avatar answered Sep 20 '22 13:09

Robert Martin