1. It takes four backslashes to match one backslash in a regular expression. Why?

I always thought that only two backslashes were needed to match a backslash, but the actual test in Java code got kicked in the face, and I went crazy and added tests, and finally concluded that four backslashes are needed to match a backslash in Java

2. Specific reasons

Special note: \ is an escape character in both Java and regular expressions

Looking at “\\\\”, the first slash is an escape, the second is the slash itself, the third is an escape, and the fourth is the slash itself.

1. A string that represents a slash needs two slashes like “\\”

2. The slash in the regular expression also needs to be escaped and is marked with “\\”.

3 So it’s relatively easy to understand

We need to represent the slash “\” in the regular expression first, and then represent it as a string. Each of these two slashes needs an escape character, so there are four slashes representing a single slash in the regular expression.

Is still learning, if there is something wrong also hope you leave a message to teach