java java分割函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5257459/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java split function
提问by ayush
Can somebody help me in understanding how split
in java works.I have the following code
有人可以帮助我了解split
Java 的工作原理吗?我有以下代码
String temp_array[];
String rates = "RF\0.6530\0.6535\D";
String temp = rates.substring(1, rates.length());
System.out.println(temp);// prints FException in thread "main" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.split(Unknown Source)
at java.lang.String.split(Unknown Source)
at simple_hello.main(simple_hello.java:15)
.6530String regex = "\\";
.6535\D
String regex = "\";
temp_array = temp.split(regex);
String insertString = "INSERT into table values("+temp_array[0]+","+temp_array[1]+","+temp_array[2]+","+temp_array[3]+")";
however at the split function i get the following exception
但是在 split 函数中,我得到以下异常
String regex = "\\";
回答by Konrad Garus
When you type "\\"
, this is actually a single backslash (due to escaping special characters in Java Strings).
当您键入 时"\\"
,这实际上是一个反斜杠(由于转义 Java 字符串中的特殊字符)。
Regular expressions also use backslash as special character, and you need to escape it with another backslash. So in the end, you need to pass "\\\\"
as pattern to match a single backslash.
正则表达式也使用反斜杠作为特殊字符,您需要使用另一个反斜杠将其转义。所以最后,您需要传递"\\\\"
模式来匹配单个反斜杠。
回答by a CVn
\
is a special character in regular expressions, as well as in Java string literals. If you want a literal backslash in a regex, you have to double it twice.Try \\\\
(becomes \\
once lexed, becomes a literal \
to the regex parser).
\
是正则表达式和 Java 字符串文字中的特殊字符。如果您想在正则表达式中使用文字反斜杠,则必须将其加倍。尝试\\\\
(\\
一旦被词法化,就成为\
正则表达式解析器的文字)。
回答by codaddict
You are trying to split the string on a back slash, you need to use:
您试图在反斜杠上拆分字符串,您需要使用:
String regex = "\\";
\
is the escape character for both Java string and the regex engine. So a Java string \\
is passed on to the regex engine as \
which is incomplete as \
has to be followed by the character that it is trying to escape.
\
是 Java 字符串和正则表达式引擎的转义字符。因此,Java 字符串\\
被传递给正则表达式引擎,因为\
它是不完整的,因为\
必须后跟它试图转义的字符。
The String \\\\
is passed on to the regex engine as \\
which is a \
escaping a \
which effectively means a literal \
.
String\\\\
被传递给正则表达式引擎,因为\\
它是一个\
转义的 a \
,它实际上意味着一个文字\
。
回答by Nishan
You have to escape '\' char in regexp patterns, so your regex basically will be
你必须在正则表达式模式中转义 '\' 字符,所以你的正则表达式基本上是
##代码##回答by Clyde Lobo
Replace
代替
String regex = "\\";
String regex = "\\";
with
和
String regex = "\\\\";
String regex = "\\\\";
回答by Manrico Corazzi
You must fix your regex. It ought to be:
您必须修复您的正则表达式。它应该是:
##代码##because the double backslash is an escape sequence for Java Strings.
因为双反斜杠是 Java 字符串的转义序列。