• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NewLine Problem in Regular Expressions

 
Bharadwaj Adepu
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read java.util.regex.Pattern, in particular the section on line terminators and the information on DOTALL.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Bharadwaj,
Your ptoblem is more with the "\" in the newline.
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No it isn't. \n is a newline character, both in chars, Strings and regular expression.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But then the .* would surely take care of that \r, wouldn't it?
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right; unless you have the UNIX_LINES flag set, \r\n is treated as one single line terminator.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic