Solved

what is the regex to match() a string against a list of words?

Posted on 2006-10-30
5
195 Views
Last Modified: 2008-02-01
Hello,
How do I pattern match a list of words against a string using String.match()? I have a function which is to search a string against a list of keywords, if the string contains any of the keywords I want the match() funciton to return true... here is what I've got:

boolean statusMessage(String msg)
{
    String regKeywords = "[failed | denied | failure | timeout]";
    return msg.matches(regKeywords);
}

Worth 500 points.

Thanks,
Rick
0
Comment
Question by:richardsimnett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 16

Expert Comment

by:Peter Kwan
ID: 17833063
You can do either of the following:

a)
boolean statusMessage(String msg) {
  String[] regKeywords = {"failed", "denied", "failure", "timeout"};
  for (int i = 0; i < regKeywords.length; i++) {
      if (msg.matches(regKeywords[i]))
        return true;
  }
  return false;
}

b)
boolean statusMessage(String msg) {
  String regKeywords = "failed,denied,failure,timeout";
  String[] regKeywordList = regKeywords.split(",");
  for (int i = 0; i < regKeywordList.length; i++) {
      if (msg.matches(regKeywordList[i]))
        return true;
  }
  return false;
}

c)
boolean statusMessage(String msg) {
  String regKeywords = "[failed | denied | failure | timeout]";
  regKeywords = regKeywords.substring(1,regKeywords.length()-1);
  String[] regKeywordList = regKeywords.split("[|]");
  for (int i = 0; i < regKeywordList.length; i++) {
      if (msg.matches(regKeywordList[i].trim()))
        return true;
  }
  return false
}
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 17833383
>>
boolean statusMessage(String msg) {
  String[] regKeywords = {"failed", "denied", "failure", "timeout"};
  for (int i = 0; i < regKeywords.length; i++) {
      if (msg.matches(regKeywords[i]))
        return true;
  }
  return false;
}
>>

That should be

boolean statusMessage(String msg) {
  String[] regKeywords = {"failed", "denied", "failure", "timeout"};
  for (int i = 0; i < regKeywords.length; i++) {
      if (msg.find(regKeywords[i]))
        return true;
  }
  return false;
}

But more performant would be simply


boolean statusMessage(String msg) {
  boolean found = false;
  String[] regKeywords = {"failed", "denied", "failure", "timeout"};
  for (int i = 0; i < regKeywords.length && !found; i++) {
      found = msg.indexOf(regKeywords[i]) >-1;
  }
  return found;
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17833393
>>That should be

No it shouldn't. You'd need to construct a Matcher and call find on it
0
 

Author Comment

by:richardsimnett
ID: 17881455
cehj,
Your performant version works very very well.

Thanks!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17881646
:-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question