php


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

Related Links

Mysql UPDATE error that only affects first row?
How to retrieve a single month for mutiple dates belonging to a month using php
Return specific items from PDOException $e
Create Only If Update isnt possible Mysql With php FOR Loop
How to remove nginx for WAMP installed on Windows OS
Pass php function to linux Variable
yii2 model str_replace() for all attributes
pdo returns true on multiple queries even if one of them fails
Insert data into MySQLi via PHP - WAMP server
Laravel-5 and Multitenancy database setup
Sql logic needed
tooltipster dynamic content does not work
i need to fetch mysql data between age
Any ideas on how to get PHP form to return to particular row/column?
Postgres.app pdo_pgsql pgsql installed but php couldn't find module. MAC OS X
Php- I tried to match an array in another array [closed]

Categories

HOME
user-interface
tomcat
facebook-messenger
magento-2.0
fogbugz
video-streaming
ag-grid
cdi
u-boot
wagtail
telegraf
specifications
parsley.js
android-externalstorage
spring-amqp
spring-jpa
varnish-vcl
tfsbuild
infrared
opam
spring-ldap
quartz.net
managed-c++
uri
envoy
nic
phpstorm-2016.1
vcenter
om-next
listjs
serversocket
creation
amazon-kms
nppexec
cups
nsuserdefaults
django-crispy-forms
google-now
slideshow
jacoco-maven-plugin
resampling
virtual-memory
music21
ssi
libreoffice-writer
sharpdx
gestures
iptv
gtk#
winmerge
yargs
yui3
wininet
between
android-sdcard
rails-engines
jeditable
gridbaglayout
m4
json-spirit
compositetype
android-handler
angular-amd
physicsjs
subview
producer
ecos
xulrunner
webaii
map
mp4parser
django-facebook
ember-table
examine
semantic-merge
daap
spark-view-engine
wndproc
live-wallpaper
getproperty
jzmq
kobold2d
navigationcontroller
quartz-core
glassfish-esb
server-variables
space-partitioning
django-pagination
bass
ning
acceleration
gui-designer

Resources

Encrypt Message