我的split()及其正则表达式出了什么问题?

时间:2022-06-01 19:59:18

Part of my application I encountered this problem. The String line variable contains 12.2 Andrew and I'm trying to split them separately but it doesn't work and comes with a NumberFormatException error. Could you guys help me on that please?

我的部分应用程序遇到了这个问题。 String line变量包含12.2 Andrew并且我试图将它们分开拆分但它不起作用并且带有NumberFormatException错误。你能帮帮我吗?

String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");

System.out.println(Double.valueOf.(data[0]));

3 个解决方案

#1


5  

Did you look at your data variable? It didn't split anything at all, since the condition never matches. You are looking for a place in the input immediately after a number and before a letter, and since there is a space in between this doesn't exist.

你看过你的数据变量了吗?它没有任何分裂,因为条件永远不会匹配。您正在输入数字之前和字母之前的输入中,并且由于之间存在空格,因此不存在。

Try adding a space in the middle, that should fix it:

尝试在中间添加一个空格,应该修复它:

String[] data = line.split("(?<=\\d) (?=[a-zA-Z])");

#2


2  

Your split is not working, and not splitting the String. Therefore Double.parseDouble is parsing the whole input.

您的拆分不起作用,并且不拆分字符串。因此Double.parseDouble正在解析整个输入。

Try the following:

请尝试以下方法:

String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
// System.out.println(Double.valueOf(data[0]));
// fixed
data = line.split("(?<=\\d).(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
System.out.println(Double.valueOf(data[0]));

Output

[12.2 Andrew]
[12.2, Andrew]
12.2

#3


2  

If you print content of data[0] you will notice that it still contains 12.2 Andrew so you actually didn't split anything. That is because your regex says:
split on place which has digit before and letter after it

如果你打印数据[0]的内容,你会发现它仍然包含12.2安德鲁,所以你实际上没有拆分任何东西。这是因为你的正则表达式是:在之前有数字并且在它之后写字的地方分开

which for data like

对于数据而言如此

123foo345bar 123 baz

effectively can only split in places marked with |

实际上只能在标有|的地方拆分

123|foo345|bar 123 baz
                  ^it will not split `123 baz` like
                   `123| baz` because after digit is space (not letter)
                   `123 |baz` before letter is space (not digit)
                   so regex can't match it

What you need is to "split on space which has digit before and letter after it" so use

你需要的是“拆分前面有数字的空格和后面的字母”这样使用

String[] data = line.split("(?<=\\d)\\s+(?=[a-zA-Z])");
//                                  ^^^^ - this represent one ore more whitespaces

#1


5  

Did you look at your data variable? It didn't split anything at all, since the condition never matches. You are looking for a place in the input immediately after a number and before a letter, and since there is a space in between this doesn't exist.

你看过你的数据变量了吗?它没有任何分裂,因为条件永远不会匹配。您正在输入数字之前和字母之前的输入中,并且由于之间存在空格,因此不存在。

Try adding a space in the middle, that should fix it:

尝试在中间添加一个空格,应该修复它:

String[] data = line.split("(?<=\\d) (?=[a-zA-Z])");

#2


2  

Your split is not working, and not splitting the String. Therefore Double.parseDouble is parsing the whole input.

您的拆分不起作用,并且不拆分字符串。因此Double.parseDouble正在解析整个输入。

Try the following:

请尝试以下方法:

String line = "12.2 Andrew";
String[] data = line.split("(?<=\\d)(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
// System.out.println(Double.valueOf(data[0]));
// fixed
data = line.split("(?<=\\d).(?=[a-zA-Z])");
System.out.println(Arrays.toString(data));
System.out.println(Double.valueOf(data[0]));

Output

[12.2 Andrew]
[12.2, Andrew]
12.2

#3


2  

If you print content of data[0] you will notice that it still contains 12.2 Andrew so you actually didn't split anything. That is because your regex says:
split on place which has digit before and letter after it

如果你打印数据[0]的内容,你会发现它仍然包含12.2安德鲁,所以你实际上没有拆分任何东西。这是因为你的正则表达式是:在之前有数字并且在它之后写字的地方分开

which for data like

对于数据而言如此

123foo345bar 123 baz

effectively can only split in places marked with |

实际上只能在标有|的地方拆分

123|foo345|bar 123 baz
                  ^it will not split `123 baz` like
                   `123| baz` because after digit is space (not letter)
                   `123 |baz` before letter is space (not digit)
                   so regex can't match it

What you need is to "split on space which has digit before and letter after it" so use

你需要的是“拆分前面有数字的空格和后面的字母”这样使用

String[] data = line.split("(?<=\\d)\\s+(?=[a-zA-Z])");
//                                  ^^^^ - this represent one ore more whitespaces