Solved

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

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

635 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