Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Pattern matching via regular expression

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Pattern matching via regular expression

    I have to match all lines having the words 'Current configuration' anywhere in the line(beginning/end/middle)

    Then in Pattern.matches(regExp, line), what must be the regExp?
    regExp = "^Current configuration" is not working.

    Thanks in advance,
    Kakoli


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Pattern matching via regular expression

    the Pattern.matches(...) must match the entire String. I'm not sure why you have the carrot, ^, in your regex. Consider adding some wild cards on both sides of your String though.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pattern matching via regular expression

    Sorry, a correction.
    I have to match all lines having the words 'Current configuration' only at the beginning of the line.

    Then in Pattern.matches(regExp, line), what must be the regExp?
    regExp = "^Current configuration" is not working.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Pattern matching via regular expression

    If the line only has Current configuration, then your text should work, but if there's text afterward, it needs to be included.

    regExp = "^Current configuration.*";
    or some such.

  5. #5
    Junior Member
    Join Date
    Dec 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pattern matching via regular expression

    Thanks, it worked!

Similar Threads

  1. Regular expression handling
    By marquiseoflight in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2012, 04:57 PM
  2. [SOLVED] Pattern, regex (regular expression) case sensitivity question
    By chronoz13 in forum Java SE APIs
    Replies: 1
    Last Post: September 7th, 2011, 04:16 AM
  3. Regular Expression pattern - complex pattern syntax
    By SmartAndy in forum Algorithms & Recursion
    Replies: 3
    Last Post: June 7th, 2011, 04:40 AM
  4. Regular Expression help
    By medoos in forum Java SE APIs
    Replies: 0
    Last Post: March 19th, 2011, 07:23 PM
  5. [SOLVED] Pattern Matching to string problme
    By Kakashi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 16th, 2011, 09:45 AM