• 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

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: 76856
366
  • 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: 76856
366
  • 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: 9734
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • 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: 76856
366
  • 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
 
brevity is the soul of wit - shakepeare. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic