Stupid regex problem

K

Koos Pol

Hi,

Can someone explain why

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

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

Thanks,
Koos
 
G

Gordon Beaton

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
 
K

Koos Pol

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
 
R

Roedy Green

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

Stefan Ram

Koos Pol said:
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
 
K

Koos Pol

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
 
S

Stefan Ram

Koos Pol said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top