• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Identifying a line feed in a String

 
Ranch Hand
Posts: 48
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This piece of code works..
Thanks everybody for your inputs.
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic