Solved

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

Posted on 2006-10-30
5
190 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
  • 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now