How to use regular expression to extract string separated by spaces?



My sample string is
"Used 1908 Honda 123 Test Coupe"

I want to extract substrings ("Used", "1908", "Honda", "123", "Test" and "Coupe") from the above string using regex. How to use the regex for same?


Regex isn’t always the answer. Depending on the programming language you use, you’d be better off splitting your string.

Say you have python, you can do something as simple as:

text = "Used 1908 Honda 123 Test Coupe"
parts = text.split(" ")

However, if you absolutely must, here’s the simplest regex for your purpose:


It simply matches anything that’s a word character (whish is a-z, A-Z, and 0-9) and is repeated once or more. It neatly excludes all whitespaces.

Answered By – M.H. Tajaddini

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More