Stupid regex problem

Discussion in 'Java' started by Koos Pol, Apr 5, 2006.

  1. Koos Pol

    Koos Pol Guest

    Hi,

    Can someone explain why

    Pattern.matches(".*", "123\r\n");

    yields false? (Removing \r\n makes it return true)

    Thanks,
    Koos
    --
    43rd Law of Computing: Anything that can go wr
    fortune: Segmentation violation -- Core dumped
     
    Koos Pol, Apr 5, 2006
    #1
    1. Advertising

  2. On Wed, 05 Apr 2006 20:19:17 +0200, Koos Pol wrote:
    > Can someone explain why
    >
    > Pattern.matches(".*", "123\r\n");
    >
    > yields false? (Removing \r\n makes it return true)


    Did you read what the Pattern class documentation says matching line
    terminators?

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 5, 2006
    #2
    1. Advertising

  3. On 5 Apr 2006 20:30:49 +0100, Gordon Beaton wrote:
    > Did you read what the Pattern class documentation says matching line
    > terminators?


    ....says *about* matching line terminators.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 5, 2006
    #3
  4. Koos Pol

    Roedy Green Guest

    On Wed, 05 Apr 2006 20:19:17 +0200, Koos Pol <> wrote,
    quoted or indirectly quoted someone who said :

    >Can someone explain why
    >
    > Pattern.matches(".*", "123\r\n");


    see http://mindprod.com/jgloss/regex.html#QUOTING
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Apr 6, 2006
    #4
  5. Koos Pol

    Koos Pol Guest

    On woensdag 05 april 2006 21:30 Gordon Beaton wrote:

    > On Wed, 05 Apr 2006 20:19:17 +0200, Koos Pol wrote:
    >> Can someone explain why
    >>
    >> Pattern.matches(".*", "123\r\n");
    >>
    >> yields false? (Removing \r\n makes it return true)

    >
    > Did you read what the Pattern class documentation says matching line
    > terminators?


    Aah, thanks Gordon. My Perl background stood in the way. In Perl the EOL
    would be irrelevant.

    Thanks again.
    Koos - Using (?s:.) now
    --
    43rd Law of Computing: Anything that can go wr
    fortune: Segmentation violation -- Core dumped
     
    Koos Pol, Apr 6, 2006
    #5
  6. Koos Pol

    Koos Pol Guest

    On donderdag 06 april 2006 01:49 Roedy Green wrote:

    >> Pattern.matches(".*", "123\r\n");

    >
    > see http://mindprod.com/jgloss/regex.html#QUOTING


    If I'm not mistaken that's about the regex itself and not the search
    string.

    Koos
    --
    43rd Law of Computing: Anything that can go wr
    fortune: Segmentation violation -- Core dumped
     
    Koos Pol, Apr 6, 2006
    #6
  7. Koos Pol

    Roedy Green Guest

    On Thu, 06 Apr 2006 18:26:31 +0200, Koos Pol <> wrote,
    quoted or indirectly quoted someone who said :

    >> see http://mindprod.com/jgloss/regex.html#QUOTING

    >
    >If I'm not mistaken that's about the regex itself and not the search
    >string.


    The regex IS the search string. The complexity comes from quoting for
    two logical levels, the regex and Java string literals, both of which
    use \ The entry covers both.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Apr 6, 2006
    #7
  8. Koos Pol

    Stefan Ram Guest

    Koos Pol <> writes:
    >Aah, thanks Gordon. My Perl background stood in the way.
    >In Perl the EOL would be irrelevant.


    It depends on the meaning of »the EOL would be irrelevant«.

    For example, in Perl 5.8.3,

    print "12345" =~ /^.*$/ ? 1 : 0;
    print "123\r4\n5" =~ /^.*$/ ? 1 : 0;

    prints

    10
     
    Stefan Ram, Apr 7, 2006
    #8
  9. Koos Pol

    Koos Pol Guest

    On vrijdag 07 april 2006 01:15 Stefan Ram wrote:

    >>In Perl the EOL would be irrelevant.

    >
    > It depends on the meaning of »the EOL would be irrelevant«.


    No it does not. In the described problem the EOL is simply irrelevant.

    print "12345" =~ /^.*$/ ? 1 : 0;
    print "123\r\n" =~ /^.*$/ ? 1 : 0;
    print "12345 " =~ /./ ? 1 : 0; # or even this
    print "123\r\n" =~ /./ ? 1 : 0; #

    prints 1111

    Cheers,
    Koos
    --
    43rd Law of Computing: Anything that can go wr
    fortune: Segmentation violation -- Core dumped
     
    Koos Pol, Apr 7, 2006
    #9
  10. Koos Pol

    Stefan Ram Guest

    Koos Pol <> writes:
    >>>In Perl the EOL would be irrelevant.

    >> It depends on the meaning of »the EOL would be irrelevant«.

    >No it does not. In the described problem the EOL is simply irrelevant.
    >print "12345" =~ /^.*$/ ? 1 : 0;
    >print "123\r\n" =~ /^.*$/ ? 1 : 0;
    >print "12345 " =~ /./ ? 1 : 0; # or even this
    >print "123\r\n" =~ /./ ? 1 : 0; #
    >prints 1111


    Insofar you are right. But the following example for Perl
    5.8.3 shows that "\r\n" is not always matched by the ".*":

    print "12345" =~ /^.*$/ ? 1 : 0;
    print "123\r\n5" =~ /^.*$/ ? 1 : 0;

    It prints »10« here. It seems as if there was a special rule
    for a trailing "\r\n", that does not apply anymore when "\r\n"
    appears in the middle of a text.

    What I sometimes use to match it, is:

    print "12345" =~ /^[^\001]*$/ ? 1 : 0;
    print "123\r\n5" =~ /^[^\001]*$/ ? 1 : 0;

    This could be used in Java as well, I assume.
     
    Stefan Ram, Apr 7, 2006
    #10
    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. Brandon McCombs
    Replies:
    4
    Views:
    529
    Richard Wheeldon
    Aug 28, 2006
  2. Replies:
    3
    Views:
    808
    Reedick, Andrew
    Jul 1, 2008
  3. rincewind

    stupid, STUPID question!

    rincewind, Apr 19, 2009, in forum: HTML
    Replies:
    25
    Views:
    1,049
  4. Leif K-Brooks

    Stupid regex problem...

    Leif K-Brooks, Nov 2, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    131
    Leif K-Brooks
    Nov 2, 2003
  5. Jason C
    Replies:
    2
    Views:
    443
    Rainer Weikusat
    Jul 18, 2012
Loading...

Share This Page