Reputation: 193
I'm trying to come up with a regex that will match only words that do not contain special characters. In the following string:
one two:two three four five:five six
I want to match one, three, four, and six only. I want to exclude the two:two and five:five.
/[a-zA-Z]+/
matches these words, but it also matches "two", "two", "five" and "five" since it just treats the ":" as a separator for another word.
Any ideas?
Upvotes: 1
Views: 5715
Reputation: 990
Alternatively, you could simply use (?<=\s+|^)[a-zA-Z]+(?=\s+|$)
. Basically, it is your original expression, but requiring
(?<=)
) of whitespace or the beginning of the string (i.e. ^
), and (?=)
) of whitespace or the end of the string (i.e. $
).That should do it for you!
Upvotes: 1
Reputation: 20129
I'm not sure what language you're using, so I'll provide a high-level overview of what to do.
Try splitting the string by whitespace (i.e., split by /\s+/
), then match each part against /^[a-zA-Z]+$/
.
Upvotes: 4