• 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

Java replace everything between two strings

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

how can I replace everything between two strings(including the strings themself) in Java?

it would look something like this: String string = text text text text text <!-- Begin Sequence text text text --> tex text text text <!-- text text text --> text text text <!-- Begin Sequence text text text -->;

I only want to delete parts that start with: <!-- Begin Sequence ... ->, but keep parts that dont contain "Begin Sequence": <!-- text text text ->.

I already tried to use .replaceAll("(?s)<!--.*?-->", "");
But it ends up deleting every '<!--' and '-->'...

Anybody got a solution to my problem? if you didnt understand what I was trying to say then please ask
 
Marshal
Posts: 79151
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Remember that Strings are not intended for replacement. Find yourself a StringBuilder, which you can create an object of using that String. Find the index of the String to its left and of the delimiter to its right and then you will have to work out how many characters to delete. You may be able to use a regular expression on the String to find the two delimiters.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should check regular expressions carefully because I think some of those characters are metacharacters. Try this tutorial. You may find only the - is a metacharacter.
 
Ranch Hand
Posts: 165
12
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I have understood your requirement correctly then you probably just need to supply the entire sequence of your match tag characters to your regexp:

That way it wont match the sections you don't want to delete.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jim Newman wrote:I already tried to use .replaceAll("(?s)<!--.*?-->", "");
But it ends up deleting every '<!--' and '-->'...


I have nothing to add to Campbell and Steffe's advice except to say that "(?s)" may not do anything for you - particularly if you read your input with readline().

And even if you don't, that "reluctant" (?) qualifier is really important since, if your string (which could be an entire file) contains many possible matches, it will choose the least expansive one.

So: putting all that together, you'd get something like:
  s.replaceAll("(?s)<!-- Begin Sequence.*?-->", "")
or possibly even:
  s.replaceAll("(?s)<!--\\s*Begin Sequence.*?-->", "")

Winston
 
Saloon Keeper
Posts: 10687
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You should check regular expressions carefully because I think some of those characters are metacharacters. Try this tutorial. You may find only the - is a metacharacter.

The hyphen (-) character is only a metacharacter when it appears inside the definition of a character set, e.g. "[a-z]".
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not in --> then? So my worries were misplaced.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Not in --> then? So my worries were misplaced.


The "range" use of '-' only happens for character expressions - ie, inside "[]".

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic