• 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

Regex | Parse | Split?

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic