tags:

views:

46

answers:

2

Getting two different words as part of in a regular expression

I want to catch say

"This Text" and "This Somethingelse"

I currently have

\s*This\sText.*

but I am trying to catch both "Text" and "Somethingelse"

I am somewhat new to regular expressions but have looked at this and am stumped. Any ideas?

+4  A: 

Try this...

\s*This\s(Text|SomethingElse).*

If you are using PHP, you can find out if it was Text or SomethingElse like so...

if (preg_match('/\s*This\s(Text|SomethingElse).*/', $text, $regs)) {
    $result = $regs[1];
    // $result will now be either "text" or "SomethingElse"
}
rikh
A: 

A sequence of "word" characters can be represented as

\w+

in most regex dialects.

Jonathan Feinberg