Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

regex - java split function

Can somebody help me in understanding how split in java works.I have the following code

String temp_array[];           
    String rates = "RF\0.6530\0.6535\D";
    String temp = rates.substring(1, rates.length());
    System.out.println(temp);// prints F.6530.6535D
    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

Exception 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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you type "", this is actually a single backslash (due to escaping special characters in Java Strings).

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...