What would the regex be to see if any of the words in a list exist in a line of text?

Hello,
What would the regex look like to see if any of the following words appeared in a line of text?

the, at, are, is, will

I just need something that will return true if any of those words are present. Im not familiar with regex at all so I was hoping someone could show me how to do it.

Thanks,
Rick
richardsimnettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ddrudikCommented:
Possibly:
Raw Match Pattern:
\b(?:the|at|are|is|will)\b
 
Java Code Example:
import java.util.regex.Pattern;
import java.util.regex.Matcher;
class Module1{
  public static void main(String[] asd){
  String sourcestring = "this is the test";
  Pattern re = Pattern.compile("\\b(?:the|at|are|is|will)\\b");
  Matcher m = re.matcher(sourcestring);
    while (m.find()){
      for( int groupIdx = 0; groupIdx < m.groupCount(); groupIdx++ ){
        System.out.println( "[" + groupIdx + "] = " + m.group(groupIdx) );
      }
    }
  }
}
 
$matches Array:
(
    [0] => Array
        (
            [0] => is
            [1] => the
        )
 
)

Open in new window

0
richardsimnettAuthor Commented:
ddrudik,
Is it safe to assume that the raw match pattern will work with String.matches() ?

Thanks,
Rick
0
richardsimnettAuthor Commented:
also is that pattern case insensitive?
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

ddrudikCommented:
No, the raw match pattern is not formatted properly for use in Java code, see the code example for the formatting as it should be (at least how I understand it should be).
0
richardsimnettAuthor Commented:
ok... what I need to be able to do is this

function boolean lineTest(String line)
{
     return ((line.substring(3) == "CHK") && (line.matches("someregex") == false));
}

I need some regex I can place into the line.matches statement that will return true if any of the words in the regex match, and it needs to be case insensitive matching because line can be upper or lower case, or contain any combination of upper or lower case.
0
ddrudikCommented:
To use .matches I would recommend you try the pattern:
"^.*\\b(?:the|at|are|is|will)\\b.*$"

you could leave the ^ and $ off of the pattern since with .matches it is assumed:
".*\\b(?:the|at|are|is|will)\\b.*"
0
ddrudikCommented:
As for case-insensitive matching you cannot specify that with .matches, you will need to convert "line" to lowercase before sending to .matches.
0
ddrudikCommented:
One option for specifying case-insensitivity inline with .matches:
".*\\b(?i:the|at|are|is|will)\\b.*"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
richardsimnettAuthor Commented:
Thanks!
0
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.