Pattern Match Question

Discussion in 'Java' started by Chris, Jun 4, 2008.

  1. Chris

    Chris Guest

    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;
    }
    }
     
    Chris, Jun 4, 2008
    #1
    1. Advertising

  2. 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
     
    Knute Johnson, Jun 4, 2008
    #2
    1. Advertising

  3. Chris

    Arne Vajhøj Guest

    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
     
    Arne Vajhøj, Jun 4, 2008
    #3
  4. Chris

    Chris Guest

    On Jun 3, 6:44 pm, Arne Vajhøj <> 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!!
     
    Chris, Jun 4, 2008
    #4
  5. Chris

    Jim Korman Guest

    Chris wrote:
    > On Jun 3, 6:44 pm, Arne Vajhøj <> 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 :)
     
    Jim Korman, Jun 4, 2008
    #5
  6. Chris

    Roedy Green Guest

    On Tue, 3 Jun 2008 18:21:04 -0700 (PDT), Chris <>
    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
     
    Roedy Green, Jun 4, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hiwa
    Replies:
    0
    Views:
    653
  2. BCC

    Pattern match question

    BCC, Aug 23, 2004, in forum: C++
    Replies:
    6
    Views:
    430
    Alex Vinokur
    Aug 24, 2004
  3. Rodney

    Pattern Match Question..

    Rodney, Sep 13, 2003, in forum: Perl Misc
    Replies:
    2
    Views:
    106
    Rodney
    Sep 14, 2003
  4. Replies:
    5
    Views:
    154
    Tad McClellan
    Jan 15, 2005
  5. Allan M. Due
    Replies:
    5
    Views:
    118
    A. Sinan Unur
    Nov 13, 2005
Loading...

Share This Page