Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 77
Java asynchronous logging 4 49
JDeveloper 12c for 32 bit 4 66
compre toata in where clue oracle 4 55
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

948 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

21 Experts available now in Live!

Get 1:1 Help Now