Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Pattern Match Question

Reply
Thread Tools

Pattern Match Question

 
 
Chris
Guest
Posts: n/a
 
      06-04-2008
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;
}
}
 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      06-04-2008
Chris wrote:
> 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();

--

Knute Johnson
email s/knute2008/nospam/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
 
Reply With Quote
 
 
 
 
Arne Vajhøj
Guest
Posts: n/a
 
      06-04-2008
Chris wrote:
> 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
 
Reply With Quote
 
Chris
Guest
Posts: n/a
 
      06-04-2008
On Jun 3, 6:44*pm, Arne Vajhøj <(E-Mail Removed)> wrote:
> Chris wrote:
> > *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



THEY BOTH WORK! THANK YOU VERY MUCH!!
 
Reply With Quote
 
Jim Korman
Guest
Posts: n/a
 
      06-04-2008
Chris wrote:
> On Jun 3, 6:44 pm, Arne Vajhøj <(E-Mail Removed)> wrote:
>> Chris wrote:
>>> 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

>
>
> 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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      06-04-2008
On Tue, 3 Jun 2008 18:21:04 -0700 (PDT), Chris <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

> 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
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A question about m/PATTERN/g - first match disappears Allan M. Due Perl Misc 5 11-13-2005 08:36 PM
newbie pattern match question :-) davidcsnow@yahoo.com Perl Misc 5 01-15-2005 01:26 AM
Pattern match question BCC C++ 6 08-24-2004 06:03 AM
Java regex can't match lengthy match? hiwa Java 0 01-29-2004 10:09 AM
Pattern Match Question.. Rodney Perl Misc 2 09-14-2003 09:46 AM



Advertisments