Regular expressions in my work (1)

Time:2021-3-4

Looking at the code directly, I can probably understand the requirements I need to complete.

Regular expressions in my work (1)

I need to be here|The last group of sign segmentation is replaced with its corresponding Chinese representation (of course, there is a corresponding one here)key-valueDictionary).

bs.replace(/([^\|]*)$/, function(key){return map[key];});

So inJavaHow to realize it?

String str = "abcd|substr";
/*1. This method doesn't support regular expressions, just simple character (string) replacement*/
String ret =  str.replace ("[^ \ \ |] * $", "replacement");
System.out.println(ret); // abcd|substr
/*2. Examples of failure*/
ret =  str.replaceAll ("[^ \ \ |] * $", "replacement");
System.out.println (RET); // ABCD | replace
/*3. New ideas*/
ret =  str.replaceFirst ("(. * \ \ |) (. *) $", "$1 replacement");
System.out.println (RET); // ABCD | replace