Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Identifying a line feed in a String

 
Manju Krishna
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a XML file which I parse and write the value of the node to a String. The XML tag is like,

<description>Search - Query Tab
Hide the Query Tab</description>

When I write this to a String I need to identify if there is any line feed in the given String and replace it with hard coded "\n" in it. (NOTE : Only if there is a line feed in it)
I tried to Pattern match to identify if there are any \r or \n.

descriptionBaseTerm is the <description> tag value



The if loop where I split the term using the same reg exp works fine returns the String as
"Search - Query Tab\n Hide the Query Tab". Which is the desired output.

Is there any other way to acheieve this?

Please help.
Thanks,
Manju
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the Pattern & Matcher is the best way, but still, How about this approach ?

Use String#split("\r|\n|\r\n") and if returned array size is greater than one, you can iterate and append the each array element.
 
Manju Krishna
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done it the same way as you have quoted.
But I was wondering why Pattern Matcher didnt work.Also wanted to know if there are any other syntax to find line feed using Patterns.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manju Krishna wrote: But I was wondering why Pattern Matcher didnt work.
But in your original post you say, it(P&M) works
Manju Krishna wrote: Also wanted to know if there are any other syntax to find line feed using Patterns.
I don't think so.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matcher.matches() checks whether the entire region is matched by the regexp, not whether the pattern matches anywhere in the region; Matcher.find() does that.
 
Manju Krishna
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reg exp which I had used in String split inside the if loop worked. i.e



match.matches() was returning false anyway
 
Manju Krishna
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ulf,
i tried Matcher find.


This is not appending the character "\n" to end of each finds. It just returns the given String in single line
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why not use String.replaceAll: This uses Pattern and Matcher (with find(), not matches()) internally.

Hmm, not even find(), but Matcher.replaceAll. That ones does use find() internally though, in a similar way as Manju's code.
 
Rob Spoor
Sheriff
Pie
Posts: 20550
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manju Krishna wrote:

This is not appending the character "\n" to end of each finds. It just returns the given String in single line

Pattern.MULTILINE is not the flag you are looking for. That will make sure that ^ and $ are found on each line. You want Pattern.DOTALL.
 
Manju Krishna
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This piece of code works..
Thanks everybody for your inputs.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Matcher.matches() checks whether the entire region is matched by the regexp, not whether the pattern matches anywhere in the region; Matcher.find() does that.

Hmm, I missed that .. Thanks Ulf.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic