• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Problem Retrieving String with new line.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

I am stuck in a very weird issue. I have a String object with XML String. e.g.


Now, what i require, is to get the data within tag <CCC></CCC> as is how you see in the above code snippet.
i.e. like this

Address : xxxx
Name : yyyy
DOB : uuuu

done

Please help..

Thanks.
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to do anything? We can discuss possible solutions (one of them using regular expressions, for example) but you need to ShowSomeEffort.
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gagan,

Welcome to CodeRanch!

As Kemal suggested, please, ShowSomeEffort and TellTheDetails - as in, did you face any issues when you tried? What have you tried yet and what issues did you face?
 
Gagan Popli
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well yes,

What i did was, to use the Pattern, Matcher and did find() on the matcher object.
That worked for <AAA> tag or <BBB> tag , as there is only one string value in them.
But since <CCC> has string with new lines, so it is not able to retrieve it. And the compiler complains
with error message "String literal is not properly closed by a double-quote"




But i wish to retrieve the <CCC>tag data as it looks above.

That's something i tried.

Thanks n Regards
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This will get you only characters in the same line with <CCC> and </CCC>.
What you need to pick is: any character, colon and whitespace (space, tab, new line, ...) between those two. So:

I would recommend you do it this way anytime you work with regular expressions. First say it in English (or whatever your native language is) what you want to match, and then jump to creating regex. Here it's pretty straightforward: any word character ( \w ), colon ( : ) and whitespace ( \s ). You just need to add asterisk ( * ) to denote that you're looking for those zero or more times (Pattern API). This will give you the desired result, also with opening and closing "CCC" tag that you can remove easily in your next step.
 
Sheriff
Posts: 22702
129
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
The original pattern is correct, but the Pattern object needs to be compiled with at least the Pattern.DOTALL flag. That makes the ".*" part include the line breaks, which isn't the case by default.

As for the "String literal is not properly closed by a double-quote" error message, you can't create String literals with line breaks in them like that. You need to replace each line break with \n (or \r\n):
 
Every snowflake is perfect and unique. And every snowflake contains a very tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic