wood burning stoves*
The moose likes Java in General and the fly likes How to add removal of newLine in a regexp pattern Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "How to add removal of newLine in a regexp pattern" Watch "How to add removal of newLine in a regexp pattern" New topic
Author

How to add removal of newLine in a regexp pattern

Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
Hello,

I have a long String coming from a different system that I am escaping using regexp using:



i.e. reserve only alphanumeric and white spaces.
The problem is that I still get the \n in the splitted array and I would like to remove it.
I can do a replaceAll("\\n", "") on each String in the array however would really prefer to do it in the original pattern before the split.

Any ideas...?

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39095
    
  23
If I remember correctly, (but you can check here and here) line end characters count as whitespace (\s).
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
Uhmmmm so there is no way to around this other than going through each stringbin the splitted array...?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check out the possible flags for java.util.regex.Pattern. I believe DOTALL is the one you need.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
DOTALL flag did the trick, thanks!
I thought that since I am not using the dot in my regexp then it will have no effect but I guess it does
Andrew Carney
Ranch Hand

Joined: Oct 17, 2006
Posts: 96
.
 
GeeCON Prague 2014
 
subject: How to add removal of newLine in a regexp pattern