1

in Java I want to remove some text from the following String via Regexp:

someText

begin
  .someMethod()
  .doSomething(TOKEN_123)
  .someMethod()
end

begin
  .someMethod()
  .doSomething(TOKEN_456)
  .someMethod()
end

begin
  .someMethod()
  .doSomething(TOKEN_789)
  .someMethod()
end

more Text

I want to remove the 2nd begin/end-Block which includes the String TOKEN_456.

Currently my regexp looks like the following

begin.*TOKEN_456(.*?)end

but this one removes the first AND second block.

Can anyone help please?

Greetz

Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
Mike G
  • 47
  • 5

1 Answers1

2

You can use

str = str.replaceFirst("(?s)begin(?:(?!begin).)*TOKEN_456.*?end\\s*", ""));

See the IDEONE demo and a regex demo.

The regex matches:

  • (?s) - a singleline modifier
  • begin - matches the leading boundary, a character sequence begin
  • (?:(?!begin).)* - a tempered greedy token that matches any text that is not starting the word begin
  • TOKEN_456 - a literal character sequence to match
  • .*?end - any number of any characters as few as possible, up to the closest end
  • \\s* - 0 or more whitespace (for trimming purposes).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • You can improve performance of the regex if you add more "anchoring" details, like word boundaries, start of *line* anchor, newlines: [`"(?sm)^begin\\b(?:(?!\\bbegin\\b).)*TOKEN_456(.*?)\nend(?:$|\n)"`](https://regex101.com/r/vN5vT7/2). – Wiktor Stribiżew Dec 04 '15 at 08:26
  • Just noticed a capturing group on `(.*?)` - I guess you can remove the parentheses if you are not interested in the captured text. – Wiktor Stribiżew Dec 04 '15 at 08:28
  • ah good to know. Thought I always have to use brackets in this context – Mike G Dec 04 '15 at 08:40