aspose file tools*
The moose likes Java in General and the fly likes NewLine Problem in Regular Expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "NewLine Problem in Regular Expressions" Watch "NewLine Problem in Regular Expressions" New topic
Author

NewLine Problem in Regular Expressions

Bharadwaj Adepu
Ranch Hand

Joined: Dec 30, 2007
Posts: 99
I am having a few lines of text like


The above mentioned text is one String.
To search this am giving the regex pattern as

This means
The line starts with //
Then followed by any character any number of times
Then followed by a new line character.
This entire group can be repeated one or more times
But this doesn't work.

And for this below text


The difference between both the texts is only the New line. The first text is multiple lines and second text is a single line.
Can any one please help me here. What is wrong with the first pattern. How to represent new line in Regex? I have tried \n, \r but nothing works there.
Please help me out


SCJP 1.5
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

Please read java.util.regex.Pattern, in particular the section on line terminators and the information on DOTALL.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Himanshu Kansal
Ranch Hand

Joined: Jul 05, 2009
Posts: 257
Hey Bharadwaj,
Your ptoblem is more with the "\" in the newline.


Experience and talent are independent of age
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

No it isn't. \n is a newline character, both in chars, Strings and regular expression.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Bharadwaj Adepu wrote:I am having a few lines of text like


The above mentioned text is one String.
To search this am giving the regex pattern as

This means
The line starts with //
Then followed by any character any number of times
Then followed by a new line character.
This entire group can be repeated one or more times
But this doesn't work.
...


It most probably does not work because your last line does not end with a new line character. You should let the end of your line be a new line character OR the end of the string:



Another possibility is that you're on Windows and the line breads are not just "\n" but "\r\n" instead.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

But then the .* would surely take care of that \r, wouldn't it?
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Rob Prime wrote:Please read java.util.regex.Pattern, in particular the section on line terminators and the information on DOTALL.


I'm not sure how that helps the OP though. S/He seems to know that the DOT does not match a new line character so using a DOT-ALL flag is not advisable here, IMO (especially not with the OP's DOT-STAR!).
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Rob Prime wrote:But then the .* would surely take care of that \r, wouldn't it?


Nope, both the '\r' and the '\n' are not matched by the DOT (without DOT-ALL enabled).
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19696
    
  20

You're right; unless you have the UNIX_LINES flag set, \r\n is treated as one single line terminator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NewLine Problem in Regular Expressions