[英]Regular Expressions with double backslash in java
I want to understand the concept of regular expression in below code: 我想在以下代码中了解正则表达式的概念:
private static final String SQL_INSERT = "INSERT INTO ${table}(${keys})
VALUES(${values})";
private static final String TABLE_REGEX = "\\$\\{table\\}";
.
.
.
String query = SQL_INSERT.replaceFirst(TABLE_REGEX, "tableName");
The above code is working fine but i would like to understand how. 上面的代码工作正常,但我想了解如何。 As per my knowledge $ and { symbols should be escaped in java string using backslash but in above string there is no backslash and if I try to add it, it shows error: invalid escape sequence. 据我所知,$和{符号应在Java字符串中使用反斜杠转义,但是在上述字符串中没有反斜杠,如果我尝试添加反斜杠,则会显示错误:无效的转义序列。
Also why the TABLE_REGEX = "\\\\$\\\\{table\\\\}";
还有为什么TABLE_REGEX = "\\\\$\\\\{table\\\\}";
contains double backslash? 包含双反斜线?
$
or "$"
is the dollar sign / a string containg it. $
或"$"
是美元符号/包含它的字符串。
\\$
is an escaped dollar sign, normally found in raw regex if you want to match the char $ instead of the end of the line \\$
是转义的美元符号,通常在原始正则表达式中找到,如果您想匹配char $而不是行尾
"\\\\$"
is a String containing an escaped \\
followed by a normale $
. "\\\\$"
是一个包含转义\\
的字符串,后跟法线$
。 Since you are not writing a raw regex, but the regex is inside a Java String
you need to escape the \\
so that when the regex interpreter comes along it just sees a normal \\
which it then treats as escaping the following $
. 由于您不是在编写原始正则表达式,而是正则表达式位于Java String
您需要对\\
进行转义,以便当正则表达式解释器出现时,它只会看到一个普通\\
,然后将其视为转义以下$
。
"\\$"
is not valid because from a normal String
point of view a $
is nothing special and does not need to / must not be escaped. "\\$"
无效,因为从正常的String
角度来看, $
没什么特别的,不需要/一定不能转义。
The $
and {
don't need to be escaped in Java string literals in general but in regular expressions they need to be escaped as they have special meaning in regular expressions. 通常,不需要在Java字符串文字中对$
和{
进行转义,但在正则表达式中则需要对它们进行转义,因为它们在正则表达式中具有特殊含义。 The $
matches the end of a line and {
is used for matching characters a certain amount of times. $
匹配行尾, {
用于匹配字符一定次数。 To match any of the regular expression special characters themselves these characters need to be escaped. 为了匹配任何正则表达式特殊字符本身,这些字符需要转义。 For example A{5}
matches AAAAA
but A\\{5
matches A{5
. 例如, A{5}
匹配AAAAA
而A\\{5
匹配A{5
。
To escape something in a regular expression string you use the \\
. 要转义正则表达式字符串中的某些内容,请使用\\
。 But the backslash in string literals itself needs escaping which is done by another \\
. 但是字符串文字中的反斜杠本身需要转义,这由另一个\\
来完成。 That is the String literal "\\\\{"
actually corresponds to the string "\\{"
. 也就是说,字符串文字"\\\\{"
实际上对应于字符串"\\{"
。
This is why in regular expression string literals you will often encounter multiple backslashes. 这就是为什么在正则表达式字符串文字中您经常会遇到多个反斜杠的原因。 You might also want to take a look at Pattern.quote(String s)
which takes a string and properly escapes all special characters (wrt. Java regular expressions). 您可能还想看看Pattern.quote(String s)
,它接受一个字符串并正确地转义所有特殊字符(wrt Java正则表达式)。
Essentially instead of 本质上代替
private static final String TABLE_REGEX = "\\$\\{table\\}";
you could write 你可以写
private static final String TABLE_REGEX = Pattern.quote("${table}");
In your example SQL_INSERT.replaceFirst(TABLE_REGEX, "tableName");
在您的示例中, SQL_INSERT.replaceFirst(TABLE_REGEX, "tableName");
matches the first occurrence of ${table}
in SQL_INSERT
and replaces this occurrence with tableName
: 匹配SQL_INSERT
的${table}
的第一个匹配项,并将此匹配项替换为tableName
:
String sql = "INSERT INTO ${table}(${keys}) VALUES(${values})".replaceFirst("\\$\\{table\\}", "tableName");
boolean test = sql.equals("INSERT INTO tableName(${keys}) VALUES(${values})");
System.out.println(test); // will print 'true'
i would like to understand how. 我想了解如何。
It is replacing the first match of the regex "\\\\$\\\\{table\\\\}"
in the original string "INSERT INTO ${table}(${keys}) VALUES(${values})"
with "tableName"
. 它用"tableName"
替换了正则表达式"\\\\$\\\\{table\\\\}"
中原始字符串"INSERT INTO ${table}(${keys}) VALUES(${values})"
的第一个匹配"INSERT INTO ${table}(${keys}) VALUES(${values})"
。
$ and { symbols should be escaped in java string using backslash but in above string there is no backslash and if I try to add it, it shows error: invalid escape sequence. $和{符号应在Java字符串中使用反斜杠转义,但在上述字符串中没有反斜杠,如果我尝试添加反斜杠,则会显示错误:无效的转义序列。
No, ${}
are not escaped in a Java string, why would they? 不, ${}
不能在Java字符串中转义,为什么呢?
Also why the TABLE_REGEX = "\\$\\{table\\}"; 还有为什么TABLE_REGEX =“ \\ $ \\ {table \\}”; contains double backslash? 包含双反斜线?
In Java escaping is done by double backslash because single backslash indicates special character (eg \\n
, \\t
). 在Java中,转义由双反斜杠完成,因为单反斜杠表示特殊字符(例如\\n
, \\t
)。 It is escaping ${}
symbols because these symbols have a special meaning in a regex, so escaping them tells the Java regex engine to treat them literally as those symbols and not their special meaning. 它转义了${}
符号,因为这些符号在正则表达式中具有特殊含义,因此转义它们会告诉Java regex引擎将它们按字面意义视为这些符号,而不是其特殊含义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.