J. Doe
J. Doe

Reputation: 13

Replace specific character sequence after specific character

I have the folowing string for example:

O > O §o TEXT §r §o TEXT §r

I need to replace all §r with §r§a only after > character.

It should be

O > O §o TEXT §r§a §o TEXT §r§a

as the result.

I tried >*(\§r) regex but it ignores >.

May You point on my error?

Upvotes: 1

Views: 110

Answers (3)

rock321987
rock321987

Reputation: 11032

A pure regex way is by using \G like following

(\G(?!\A)|>)(.*?)§r

and, replace with

$1$2§r§a

Regex Demo

Java Code

System.out.println("O§r §r > O §o TEXT §r §o §r TEXT §r".replaceAll("(\\G(?!\\A)|>)(.*?)§r", "$1$2§r§a"));

Ideone Demo

Upvotes: 0

Ro Yo Mi
Ro Yo Mi

Reputation: 15000

Description

((?:(?!>).)*>.*?|)(§r)

Regular expression visualization

Replace With: $1§r§a

** To see the image better, simply right click the image and select view in new window

Example

Live Demo

https://regex101.com/r/xP8dI5/1

Sample text

§r O > O §o TEXT §r §o TEXT §r

After Replacment

§r O > O §o TEXT §r§a §o TEXT §r§a

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
----------------------------------------------------------------------
        >                        '>'
----------------------------------------------------------------------
      )                        end of look-ahead
----------------------------------------------------------------------
      .                        any character except \n
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
    >                        '>'
----------------------------------------------------------------------
    .*?                      any character except \n (0 or more times
                             (matching the least amount possible))
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    §r                      '§r'
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------

Upvotes: 1

Leo Izen
Leo Izen

Reputation: 4289

The easiest way to do this would be to split it into two strings first and then run a replace. That is, you could take

int index = inputString.indexOf('>') + 1;
String first = inputString.subString(0, index);
String second = inputString.subString(index);
String finalString = first + second.replace("§r", "§r§a");

Doing this with a pure regular expression would be difficult.

Upvotes: 1

Related Questions