Java regex for Uk postcodes with spaces -
this question has answer here:
- uk postcode regex (comprehensive) 26 answers
i'm trying create regex matching following patterns (with , without space):
m1 1aa, m60 1nw, cr2 6xh, dn55 1pt, w1a 1hq , ec1a 1bb
i'm new @ , find hard create functional regex examples above.
searching here , there found regex might work of patterns don't know how add condition "with or without space" each type of postcode.
here regex found on post "^(a-pr-uwyz [0-9][abd-hjlnp-uw-z]{2})"
how add space/no space condition? in order match m11aa or m1 1aa.
you need regex:
^([a-pr-uwyz](([0-9](([0-9]|[a-hjkstuw])?)?)|([a-hk-y][0-9]([0-9]|[abehmnprvwxy])?)) ?[0-9][abd-hjlnp-uw-z]{2})$ ^
this space must set optional ?
quantifier means 0 or 1 repetition.
see demo
Comments
Post a Comment