azhagu
azhagu

Reputation: 71

Address Parsing PHP Regex

I got struct with to parse the Door number/Flat number from address. So kindly guide me how to do this using regex. Also i have tried with following regex to parse that but it doesn't works what we expected.

preg_match('![0-9/-]+!', $address, $matches);

Also i have added few sample inputs and expected output

Input
#302 MEENA RESIDENCY NEW ALLAPUR 600032
Expected Output
302

Input
No 35/2 2nd main 2nd cross subramanium l ayout viginapura ramurthy nagar 600032
Expected Output
35/2

Input
17-13-54 jonnaguddi vzm,near bhashyam school 600032
Expected Output
17-13-54

Input
Floor 4,plot no 285 2 birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
285

Input
3-a  birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3-a

Input
3a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3a

Input
3/a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3/a

Input
3/2a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3/2a

Input
3a/2 birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3a/2

Input
3/2-a birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3/2-a

Input
3-a/2 birla mansion d d sathe marg prathana samaj girgaon mumbai 600032
Expected Output
3-a/2

Thank you

Upvotes: 2

Views: 65

Answers (2)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626774

You may use

~(?:#|\bno\s*|^)(\d+(?:[/-]?\w+)*)~im

or

~(?:#|\bno\s*|^)\K\d+(?:[/-]?\w+)*~im

See the regex demo

Details:

  • (?:#|\bno\s*|^) - either of:
    • # - a hash sign
    • \bno\s* - a whole word followed with 0+ whitespaces
    • ^ - start of a line
  • \K - a match reset operator discarding the text matched so far in the current iteration
  • \d+ - 1+ digits
  • (?:[\/-]?\w+)* - 0+ sequences of:
    • [\/-]? - an optional / or -
    • \w+ - 1+ letters/digits/_

Upvotes: 1

Niitaku
Niitaku

Reputation: 835

Expression

This regex selects the desired numbers in group 1:

/^(?:#|.*?\bno\s+)?(\d[\w\-\/]*)/gmi

It performs the same selection as Wiktor Stribizew's one, but in less steps.

Here is a demo

Explanation

  • ^ asserts the start of a line
  • (?:#|.*?\bno\s+)? indicates the number is possibly preceded by:
    • a # character
    • a word no followed by at least one space
  • (\d[\w\-\/]*) captures the number that start with a number and can be composed of a letter, a number, a dash - or a slash /.

If you test your string one at a time, the m flag (multiline) isn't necessary.

Upvotes: 0

Related Questions