tags:

views:

36

answers:

2

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?

+4  A: 

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]+$/.

Ryan Mentley
+1  A: 

Alternatively, you could simply use (?<=\s+|^)[a-zA-Z]+(?=\s+|$). Basically, it is your original expression, but requiring

  • a prefix (i.e. (?<=)) of whitespace or the beginning of the string (i.e. ^), and
  • a suffix (i.e. (?=)) of whitespace or the end of the string (i.e. $).

That should do it for you!

0x24a537r9