# 【已解决】Java的正则表达式java.util.regex中匹配星号’*’ asterisk字符本身

Java regex 6732浏览

【问题】

\*

illegal escape character

Dangling meta character ‘*’

【解决过程】

1.去官网的Java的正则表达式的官网:

java.util.regex.Pattern

2.后来参考:

java 的转义字符，在正则表达式中应用

3.后来参考：

Match asterisk with regexp

\\*

Backslashes, escapes, and quoting

The backslash character (‘\’) serves to introduce escaped constructs, as defined in the table above, as well as to quote characters that otherwise would be interpreted as unescaped constructs. Thus the expression \\ matches a single backslash and \{ matches a left brace.

It is an error to use a backslash prior to any alphabetic character that does not denote an escaped construct; these are reserved for future extensions to the regular-expression language. A backslash may be used prior to a non-alphabetic character regardless of whether that character is part of an unescaped construct.

Backslashes within string literals in Java source code are interpreted as required by the

Java Language Specification as either Unicode escapes or other character escapes. It is therefore necessary to double backslashes in string literals that represent regular expressions to protect them from interpretation by the Java bytecode compiler. The string literal "\b", for example, matches a single backspace character when interpreted as a regular expression, while "\\b" matches a word boundary. The string literal "\(hello\)" is illegal and leads to a compile-time error; in order to match the string (hello) the string literal "\\(hello\\)" must be used.

java中的反斜杠呢，是被Java语言中定义为了，专门用于表示Unicode字符或者其他转义字符的。

\b，本身表示的是一个后退键，是单个（特殊的控制）字符；

\\b，就是正则表达式中的了，表示单词边界（word boundary）

【总结】

\d -> \\d

\w -> \\w

86 queries in 0.151 seconds, using 20.69MB memory