Pattern Match Question

C

Chris

All,
I have a question regarding pattern matching. In my class below an
argument String is taken in and compared to the name ‘chris’. If
‘chris’ is the only word in String s then the pattern checks out okay.
However, I’m wondering if it is possible to enter a sentence worth of
words and if ‘chris’ happens to be one of those words in the sentence
then the return is true otherwise false.

Thank you very much for your time.
______________________________________________________________

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameCheck {

public static boolean patternMethod(String s) {
System.out.println(s);
Pattern p = Pattern.compile("chris");
Matcher m = p.matcher(s);
boolean isOk = m.matches();
return isOk;
}
}
 
K

Knute Johnson

Chris said:
All,
I have a question regarding pattern matching. In my class below an
argument String is taken in and compared to the name ‘chris’. If
‘chris’ is the only word in String s then the pattern checks out okay.
However, I’m wondering if it is possible to enter a sentence worth of
words and if ‘chris’ happens to be one of those words in the sentence
then the return is true otherwise false.

Thank you very much for your time.
______________________________________________________________

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NameCheck {

public static boolean patternMethod(String s) {
System.out.println(s);
Pattern p = Pattern.compile("chris");
Matcher m = p.matcher(s);
boolean isOk = m.matches();
return isOk;
}
}

m.find();
 
A

Arne Vajhøj

Chris said:
I have a question regarding pattern matching. In my class below an
argument String is taken in and compared to the name ‘chris’. If
‘chris’ is the only word in String s then the pattern checks out okay.
However, I’m wondering if it is possible to enter a sentence worth of
words and if ‘chris’ happens to be one of those words in the sentence
then the return is true otherwise false.
public static boolean patternMethod(String s) {
System.out.println(s);
Pattern p = Pattern.compile("chris");
Matcher m = p.matcher(s);
boolean isOk = m.matches();
return isOk;
}

Try:

Pattern p = Pattern.compile(".*\\bchris\\b.*");

Arne
 
J

Jim Korman

Chris said:
THEY BOTH WORK! THANK YOU VERY MUCH!!
They do...But understand the difference between find() and matches(),
It's important

Jim (who learned the hard way :)
 
R

Roedy Green

If
‘chris’ is the only word in String s then the pattern checks out okay.
However, I’m wondering if it is possible to enter a sentence worth of
words and if ‘chris’ happens to be one of those words in the sentence
then the return is true otherwise false.

See the section on regex match vs find vs looking at.

See http://mindprod.com/jgloss/regex.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,998
Latest member
MarissaEub

Latest Threads

Top