I want to replace special characters between the following String when only when they are between two words, BEGIN and END . xxx to CCC and yyy to DDD, using Java regular expression or some way ? can you help
Raw string = "John Doe xxx Amazing man BEGIN reference xxx yes yyy indeed this is true xxx
no yyy END , so this xxx does not change"
converted String = "John Doe xxx Amazing man BEGIN reference CCC yes DDD indeed this is true
CCC no DDD END, so this xxx does not change"
You need to define your pattern and matcher:
Pattern MY_PATTERN = Pattern.compile("BEGIN" + "([ ]*+[0-9A-Za-z]++[ ]*+)*" + "xxx" + "([ ]*+[0-9A-Za-z]++[ ]*+)*" + "END");
Matcher m = MY_PATTERN.matcher(rawString);
Then you will call the find method on the matcher and every time it finds what you wanted will replace it with what you needed:
while (m.find()) {
rawString = rawString.replaceFirst(m.group(0),m.group(0).replaceAll("xxx","CCC"));
}
Even though your sample data displays "xxx"
before "yyy"
, there's no specification if "xxx"
is before "yyy"
or vice versa. You just state, "special characters between the following String when only when they are between two words, BEGIN and END"
.
I see having a Map<String, String>
where the keys are your "special" strings and the values are the strings that'll replace the "special" strings.
Iterate through this map and provide the keys to this:
String.format("BEGIN.*?(%s).*?END", kvp.getKey())
In this example, it'll produce two regex patterns:
"BEGIN.*?(xxx).*?END"
"BEGIN.*?(yyy).*?END"
This will capture your "special" strings into capture group 1, which you'll provide to String.replace()
like so:
raw = raw.replace(matcher.group(), matcher.group().replace(matcher.group(1), kvp.getValue()));
matcher.group()
is the entire matching string BEGIN ... END
and matcher.group(1)
will either be xxx
or yyy
Put this all together and you have:
public static void main(String[] args) throws Exception {
Map<String, String> replacerMap = new HashMap() {{
put("xxx", "CCC");
put("yyy", "DDD");
}};
String raw = "John Doe xxx Amazing man BEGIN reference xxx yes yyy indeed this is true xxx no yyy END , so this xxx does not change";
System.out.println("Before: ");
System.out.println(raw);
System.out.println();
for (Map.Entry<String, String> kvp : replacerMap.entrySet()) {
Matcher matcher = Pattern.compile(String.format("BEGIN.*?(%s).*?END", kvp.getKey())).matcher(raw);
if (matcher.find()) {
raw = raw.replace(matcher.group(), matcher.group().replace(matcher.group(1), kvp.getValue()));
}
}
System.out.println("After: ");
System.out.println(raw);
}
Results:
Before:
John Doe xxx Amazing man BEGIN reference xxx yes yyy indeed this is true xxx no yyy END , so this xxx does not change
After:
John Doe xxx Amazing man BEGIN reference CCC yes DDD indeed this is true CCC no DDD END , so this xxx does not change
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.