HJK
HJK

Reputation: 1382

How to identify 4th occurance of a character using java regex

I have two Strings like

 aisozp;ak  apso;;   ;pasix acd;XYZ;ao aoz;;  ;aixi o  oiz
 aisozp;ak  apso;;XYZ;pasix acd;098;ao aoz;;  ;XYZ; as oiz

Here I need to replace the string XYZ with ABC,only when it exists in between 5th and 6th semicolon (;). I tried the following thing to achieve this

 data.replaceAll("(((.*?);){5})XYZ", "$1ABC")

It is working fine with the first string. But for the second string, it is replacing the string XYZ (last occurrence) which is not followed by 5th semicolon. Here how to restrict the search to 5. Above regex is trying to match XYZ from semicolon 5 and above I guess. Note: data length between each semicolon is variable. Could someone help me on this?

Thanks in advance.

Upvotes: 0

Views: 78

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174816

You need to specify the anchor ^ which denotes the starting point.

Regex:

^((?:[^;]*;){5})XYZ

Replacement string:

$1ABC

DEMO

String s = " aisozp;ak  apso;;   ;pasix acd;XYZ;ao aoz;;  ;aixi o  oiz\n" + 
        " aisozp;ak  apso;;XYZ;pasix acd;098;ao aoz;;  ;XYZ; as oiz";
String m = s.replaceAll("^((?:[^;]*;){5})XYZ", "$1ABC");
System.out.println(m);

Output:

 aisozp;ak  apso;;   ;pasix acd;ABC;ao aoz;;  ;aixi o  oiz
 aisozp;ak  apso;;XYZ;pasix acd;098;ao aoz;;  ;XYZ; as oiz

Upvotes: 2

Florent Bayle
Florent Bayle

Reputation: 11930

That's because your . in .* matches any character, including ;.

Replace the . with [^;] to match any character, except ;, and add a ^ at the beginning, to force the matching of the String from the start:

data.replaceAll("^((([^;]*?);){5})XYZ", "$1ABC")

Upvotes: 5

Related Questions