If you want to match a specific and defined set of words, you can have them fully typed out and separated with the OR operator, |
:
(am|not|smart)
Pending the language you're using, you'll need to specify different flags to capture them individually, but "all" of them. In javascript, for instance, you would use g
:
str.match(/(am|not|smart)/g);
Whereas in PHP you would simply use the preg_match_all()
function:
preg_match_all('/(am|not|smart)/', $str, $matches);
If you're looking to match "all words", i.e. "any word", you can use the word-boundary \b
:
\b([a-zA-Z]+)\b
This, of course, can be modified to accept punctuation or numeric values as well.
Regarding your second question, you hinted at the ability to do it in the first with a matching-set (i.e. characters within brackets). To capture any a
or b
character followed by anything else:
([ab].*)
If you want them to have to be followed by other letters (which can be expanded from here):
([ab][a-z]+)