Solved

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

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
jdbc error in jsp application 20 64
JAVA API design with micro service cloud in mind 1 49
ejb mdb examples 1 10
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

821 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