Assuming your regex mechanism supports lookahead / lookbehind statements, you could use something like the following:
(^|(?<= )[a-zA-Z]+($|(?= ))
Here is a brief description of what each component does:
(^|(?<= )) : This says: "If the word begins here, we are interested." In particular,
^ : match the beginning of a line or (?<= ) : match any point preceded by a space, without actually consuming the space itself. This is called a positive lookbehind statement.
[a-zA-Z]+ : This should be obvious, but it matches any run of consecutive ASCII letters.
($|(?= )) : This says: "If the word is finished here, we are finished." In particular,
$ : match end of line, or
(?= ) : match any point followed by a space without actually consuming the space itself. This is called a positive statement.
Note that this particular regular expression does not count a word as a word if it follows punctuation. It may not be the way you want it, but you have described checking for spaces so that it is performed by a regular expression. If you want to support words followed by simple punctuation, you can change what the last atom will be
($|(?=[ .,!?]))
which will match the word if followed by a space, period, comma, exclamation mark or question mark. You can be more complicated if you want.
source share