This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex | Parse | Split?

 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an application that will look through files and grab a string out of file that is bracketed by known special characters.

Example: XXX The sky is blue XXX

Needed Result: The sky is blue

I tried a replaceAll and the characters on the line previous to the special characters were not removed. I need to parse everything before and everything after to leave me with only what is in between.

Does anyone have an idea how to do this? I am using java 1.4

Thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But, your string may contain more than one token separated by this XXX, right?

XXX The sky is blue XXX The blood is red XXX The sun is yellow XXX

And the separator is XXX, right?

Are the empty spaces after and before the XXX token part of the token or part of the separator?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49776
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need a regular-expression-using method, one of which is String#split (introduced in J2SE1.4). You will probably have to read about regular expressions in the Java™ Tutorials.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
[QB]But, your string may contain more than one token separated by this XXX, right?

XXX The sky is blue XXX The blood is red XXX The sun is yellow XXX

And the separator is XXX, right?


The seperator is anything unique that can be set within Ant code. The application runs Ant scripts. I am doing a echo and want to pick up entries not in all echos <echo> but within an echo and between any given special identifires that will not conflict with xml. I don't know if that makes sense.
[ May 16, 2008: Message edited by: James Daniel ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a regex like

"XXX(.*?)XXX"

Find all matches. The content of the group is what you want to extract.

Can you tell us more about what you are trying to accomplish with this?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic