Home > Back-end >  Python Regex some name US Address
Python Regex some name US Address

Time:10-23

I have these kind of strings:

WILLIAM SMITH 2345 GLENDALE DR RM 245 ATLANTA GA 30328-3474
LINDSAY SCARPITTA 655 W GRACE ST APT 418 CHICAGO IL 60613-4046

I want to make sure that strings I will get are like those strings like above.
Here's my regular expression:

[A-Z]  [A-Z]  [0-9]{3,4} [A-Z]  [A-Z]{2,4} [A-Z]{2,4} [0-9]  [A-Z]  [A-Z]{2} [0-9]{5}-[0-9]{4}$

But my regular expression only matches the first example and does not match the second one.

CodePudding user response:

Try this:

^[A-Z] [ \t] [A-Z] [ \t] \d .*[ \t] [A-Z]{2}[ \t] \d{5}(?:-\d{4})$

Demo

CodePudding user response:

Here's dawg's regex with capturing groups:

^([A-Z] [ \t] [A-Z] )[ \t] (\d )[ \t](.*)[ \t] ([A-Z]{2})[ \t] (\d{5}(?:-\d{4}))$

Here's the url.

UPDATE

sorry, I forgot to remove non-capturing group at the end of dawg's regex...
Here's new regex without non-capturing group: regex101

  • Related