Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Stupid regex problem

Thread view: 
Koos Pol - 05 Apr 2006 19:19 GMT
Hi,

Can someone explain why

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

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

Thanks,
Koos
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Gordon Beaton - 05 Apr 2006 19:34 GMT
> 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

Signature

[  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 - 05 Apr 2006 19:34 GMT
> Did you read what the Pattern class documentation says matching line
> terminators?

...says *about* matching line terminators.

/gordon

Signature

[  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

Koos Pol - 06 Apr 2006 17:06 GMT
>> Can someone explain why
>>
[quoted text clipped - 4 lines]
> 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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Stefan Ram - 07 Apr 2006 00:15 GMT
>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

 .
Koos Pol - 07 Apr 2006 16:57 GMT
>>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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Stefan Ram - 07 Apr 2006 18:03 GMT
>>>In Perl the EOL would be irrelevant.
>>   It depends on the meaning of »the EOL would be irrelevant«.
[quoted text clipped - 4 lines]
>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.
Roedy Green - 06 Apr 2006 00:49 GMT
>Can someone explain why
>
>    Pattern.matches(".*", "123\r\n");

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

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Koos Pol - 06 Apr 2006 17:26 GMT
>>    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
Signature

43rd Law of Computing: Anything that can go wr
fortune: Segmentation violation -- Core dumped

Roedy Green - 06 Apr 2006 21:36 GMT
>> 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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.