Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

regex101: How can I capture the street address on each line in a more economical way? I am new to Regex

I am trying to capture the street on each line of text on the regex101.com, but the
address on line 2; "4241 Jerry Dove Drive" and line 6; "946 Douglas Dairy Road" are
giving me problems because they are the only 3 word street names. There must be a better way of writing this!

expression:

\s(\d{2,4}\s[A-Z]\w*\s[A-Z]\w*\sD?r?i?v?e?R?o?a?d?)

This is the txt file:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Jazmine Holcomb 3212 Adams Avenue Washington MD eluir.azevedo.7o@lucidmode.com
Sofie Hagan 4241 Jerry Dove Drive Erie PA zfraction00t@jfaccupuncture.com
Cairo Tyson 3768 Clifford Street San_Jose CA varifi.amin@disabilitylawyersatlanta.com
Tasmin Kearney 2956 Adams Drive El_Campo CA 1paul.clinssonq@aprilmovo.com
Aydin Moran 3727 Sarah Drive Lake_Charles LA 3egdol@blogspot.miami
Samirah Pollard 946 Douglas Dairy Road Prosperity SC rzekohahaha@gmailup.com
Jaskaran Wheeler 1521 Richards Avenue Torrance CA ardit.ditty@netfacc.com
Gerrard Browning 4690 Felosa Drive Los_Angeles CA cgbeshar@lotomoneymaker.com
Haleema Craft 73 Pinchalone Street Norfolk VA 0kjhdsxz123e@summitgg.com
Brett Neal 4079 Johnson Street Garner NC fali_gx2000w@plussmail.com

    

>Solution :

This regex could suit you:

\s(\d{2,4}(?:\s[A-Z]\w*){1,3}\s(?:Street|Drive|Avenue|Road))

It contains a non-capturing group (?: ) for the words before the final word, and says have 1 to 3 of such words, and then as last word it needs one of

  • Street
  • Drive
  • Avenue
  • Road

You need to enclose that last word in a non-capturing group as well, otherwise, the first variant would be the 1 to 3 words + "Street" and the other variants would be just "Drive", "Avenue" or "Road" without the words before.

If you like, you could increase the number of words before the final word by any number, or up to infinity by just having a + (one or more) instead of the {1,3} (any number between 1 and 3)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading