java.util.regex ^ problem

Discussion in 'Java' started by Glenn Meter, Sep 21, 2004.

  1. Glenn Meter

    Glenn Meter Guest

    I'm trying to use the regex package to match strings at the beginning
    of lines within files. However, it looks like ^ is only matching on
    items at the start of the string, not at the start of a line.

    This sets b to true, with m.start() returning 0 [the beginning of text]:

    Pattern p = Pattern.compile("^foo");
    Matcher m = p.matcher("foo\nmatch bar\n");
    boolean b = m.find();

    This leaves b as false:

    Pattern p = Pattern.compile("^match");
    Matcher m = p.matcher("foo\nmatch bar\n");
    boolean b = m.find();

    Since the top test succeeds, it looks like it's taking '^' as a
    boundary marker instead of the "not this character" marker.
    The fact that RegexTestHarness only checks one line at a time isn't
    giving me great confidence that multi-line input was thoroughly tested.

    Any clues would be greatly appreciated.

    Thanks,
    Glenn
    Glenn Meter, Sep 21, 2004
    #1
    1. Advertising

  2. Glenn Meter

    Chris Smith Guest

    Glenn Meter wrote:
    > I'm trying to use the regex package to match strings at the beginning
    > of lines within files. However, it looks like ^ is only matching on
    > items at the start of the string, not at the start of a line.


    From the API documentation for java.util.Pattern:

    By default, the regular expressions ^ and $ ignore line terminators
    and only match at the beginning and the end, respectively, of the
    entire input sequence. If MULTILINE mode is activated then these
    expressions match just after or just before, respectively, a line
    terminator or the end of the input sequence with the exception that
    the expression ^ never matches at the end of input, even if the last
    character is a newline.

    So try:

    Pattern p = Pattern.compile("^match", Pattern.MULTILINE);
    Matcher m = p.matcher("foo\nmatch bar\n");
    boolean b = m.find();

    I haven't tried it, but I suspect it will work.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, Sep 21, 2004
    #2
    1. Advertising

  3. Glenn Meter

    Glenn Meter Guest

    That did the trick. Next time I'll RTFM more carefully.

    Thanks,
    Glenn


    On 2004-09-20 23:43:48 -0500, Chris Smith <> said:

    > From the API documentation for java.util.Pattern:
    >
    > By default, the regular expressions ^ and $ ignore line terminators
    > and only match at the beginning and the end, respectively, of the
    > entire input sequence. If MULTILINE mode is activated then these
    > expressions match just after or just before, respectively, a line
    > terminator or the end of the input sequence with the exception that
    > the expression ^ never matches at the end of input, even if the last
    > character is a newline.
    >
    > So try:
    >
    > Pattern p = Pattern.compile("^match", Pattern.MULTILINE);
    > Matcher m = p.matcher("foo\nmatch bar\n");
    > boolean b = m.find();
    >
    > I haven't tried it, but I suspect it will work.
    Glenn Meter, Sep 21, 2004
    #3
    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. dhek bhun kho

    java.util.regex: Backreferences?

    dhek bhun kho, Jul 9, 2003, in forum: Java
    Replies:
    2
    Views:
    782
    dhek bhun kho
    Jul 9, 2003
  2. Roedy Green

    Re: java.util.regex.Matcher

    Roedy Green, Jul 30, 2003, in forum: Java
    Replies:
    0
    Views:
    369
    Roedy Green
    Jul 30, 2003
  3. Calum MacLean

    java.util.regex question

    Calum MacLean, Nov 28, 2003, in forum: Java
    Replies:
    3
    Views:
    419
    John C. Bollinger
    Dec 1, 2003
  4. Alex Polite
    Replies:
    14
    Views:
    959
  5. joes
    Replies:
    2
    Views:
    1,004
    Daniel Pitts
    May 25, 2007
Loading...

Share This Page