| Author |
Regex | Parse | Split?
|
James Daniel
Ranch Hand
Joined: Sep 24, 2004
Posts: 78
|
|
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
Joined: Dec 31, 2004
Posts: 961
|
|
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
Joined: Oct 13, 2005
Posts: 32654
|
|
|
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
Joined: Sep 24, 2004
Posts: 78
|
|
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
Joined: Jul 11, 2001
Posts: 14112
|
|
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?
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
 |
|
|
subject: Regex | Parse | Split?
|
|
|