I usually don’t care much about the implementation of the code, because my development language is very messy and tends to be the simplest and most generic way to solve the problem. Today I accidentally saw a friend in the group posted the following Java code, I feel very limited and ignorant:

  


As you can see from the output, the function of this code is to replace the camel named string with the underlined split. This function is relatively simple, but it is his code that attracts me.

createTime.replaceAll(([A-Z]+),_$1)

This line of code is as simple as calling the replaceAll method of the String class. The method takes the regular expression as its first argument and the new value to be replaced with as its second argument.

To my surprise, the second argument to replaceAll in his code, which was called replacement in the JDK documentation, was _$1. What the hell is this? And support for things like placeholders? I never knew.

To explore problems

Since I studied regular expressions earlier, by looking at the first argument ([a-z]+) to replaceAll, I assumed that this was A grouping of regular expressions, corresponding to the java.util.regex.matcher class’s group() method in JDK.

In Linux Sed command, the use of some substitution, the principle should be the same.

Take a look at how the String.replaceAll method is implemented. JDK:

  


It uses Matcher at the bottom, but with Matcher’s own replaceAll method.

If you look at its documentation, the argument to this method is a ghost, see the implementation code below.

  


The key part of the documentation is the appendReplacement method, and then you can see the detailed description of the documentation.

It is clear that the replacement parameter of this method can be used to refer to the grouping obtained by Matcher through the regular matching through $character, which supports name and number. The corresponding methods here are group(name) and group(int) of the Matcher class.

conclusion

1. The replaceAll method of String is actually implemented through the replaceAll() method of the java.util.regex.matcher class.

2. The replaceAll method of the java.util.regex.Matcher class again implements the replacement logic by calling the appendReplacement method

3. The replacement parameter to the appendReplacement method of the Matcher class supports a $sign to refer to the Matcher matched grouping

The following line of code is a best practice for grouping using Matcher classes.

  


Group (0) represents the entire string

Group (1) represents the first match, which in the example above is the (my mobile number is :([0-9]{11})) part

Group (2) represents the second match, which in the example above is the ([0-9]{11}) part

Use groups can be used to extract the target string value in the string, very easy to use!

A few examples

Here are a few examples that you can relate to by analogy.

Name the hump by underlining

  


Underline to beijin

This one is a little trickier, written by the copycat matcher. replaceAll method.

public static String underlineToCamel(String underlineName) {

Matcher matcher = Pattern.compile((_[a-z]{1})).matcher(underlineName);

StringBuffer result = new StringBuffer();

while (matcher.find()) {

String replacement = matcher.group(1);

matcher.appendReplacement(result, replacement.replace(_, ).toUpperCase());

}

matcher.appendTail(result);

return result.toString();

}

In addition, Mybatis Generator plug-in also provides the similar method in the source of (JavaBeansUtil. GetCamelCaseString), here to do the simple changes

  


The speed is significantly faster without the involvement of complex regex.

End

Looking at some good open source code, you can actually learn some useful tips. It’s much more efficient than having to reinvent the wheel yourself. Time is on the line, but MIIEILE