I have a string that matches this regular expression: ^.+:[0-9]+(\.[0-9]+)*/[0-9]+$
which can easily be visualized as (Text):(Double)/(Int)
. I need to split this string into the three parts. Normally this would be easy, except that the (Text)
may contain colons, so I cannot split on any colon - but rather the last colon.
我有一个匹配这个正则表达式的字符串:^。+:[0-9] +(\。[0-9] +)* / [0-9] + $可以很容易地显示为(文本):(双)/(INT)。我需要将这个字符串拆分为三个部分。通常这很容易,除了(文本)可能包含冒号,所以我不能拆分任何冒号 - 而是最后一个冒号。
The .*
is greedy so it already does a pretty neat job of doing this, but this wont work as a regular expression into String.split() because it will eat my (Text)
as part of the delimiter. Ideally I'd like to have something that would return a String[] with three strings. I'm 100% fine with not using String.split() for this.
。*是贪婪的,所以它已经做了一个相当干净的工作,但这不会作为String.split()的正则表达式,因为它将吃我的(文本)作为分隔符的一部分。理想情况下,我想要一些能够返回带有三个字符串的String []的东西。我没有使用String.split()就可以100%罚款。
3 个解决方案
#1
3
Why don't you just use a straight up regular expression?
你为什么不直接用正则表达式?
Pattern p = Pattern.compile("^(.*):([\\d\\.]+)/(\\d+)$");
Matcher m = p.matcher( someString );
if (m.find()) {
m.group(1); // returns the text before the colon
m.group(2); // returns the double between the colon and the slash
m.group(3); // returns the integer after the slash
}
Or similar. The pattern ^(.*):([\d\.]+)/(\d+)$
assumes that you actually have values in all three positions, and will allow just a period/fullstop in the double position, so you may want to tweak it to your specifications.
或类似的。模式^(。*):( [\ d \。] +)/(\ d +)$假设您实际上在所有三个位置都有值,并且只允许双倍位置的句点/句点,所以你可以想要根据您的要求进行调整。
#2
5
I don't like regex (just kidding I do but I'm not very good at it).
我不喜欢正则表达式(只是开玩笑,但我不是很擅长)。
String s = "asdf:1.0/1"
String text = s.substring(0,s.lastIndexOf(":"));
String doub = s.substring(s.lastIndexOf(":")+1,text.indexOf("/"));
String inte = s.substring(text.indexOf("/")+1,s.length());
#3
1
String.split()
is typically used in simpler scenarios where the delimiter and formatting are more consistent and when you don't know how many elements you are going to be splitting.
String.split()通常用于更简单的场景,其中分隔符和格式更一致,并且当您不知道要分割多少元素时。
Your use case calls for a plain old regular expression. You know the formatting of the string, and you know you want to collect three values. Try something like the following.
您的用例需要一个普通的旧正则表达式。您知道字符串的格式,并且您知道要收集三个值。尝试类似以下的内容。
Pattern p = Pattern.compile("(.+):([0-9\\.]+)/([0-9]+)$");
Matcher m = p.matcher(myString);
if (m.find()) {
String myText = m.group(1);
String myFloat = m.group(2);
String myInteger = m.group(3);
}
#1
3
Why don't you just use a straight up regular expression?
你为什么不直接用正则表达式?
Pattern p = Pattern.compile("^(.*):([\\d\\.]+)/(\\d+)$");
Matcher m = p.matcher( someString );
if (m.find()) {
m.group(1); // returns the text before the colon
m.group(2); // returns the double between the colon and the slash
m.group(3); // returns the integer after the slash
}
Or similar. The pattern ^(.*):([\d\.]+)/(\d+)$
assumes that you actually have values in all three positions, and will allow just a period/fullstop in the double position, so you may want to tweak it to your specifications.
或类似的。模式^(。*):( [\ d \。] +)/(\ d +)$假设您实际上在所有三个位置都有值,并且只允许双倍位置的句点/句点,所以你可以想要根据您的要求进行调整。
#2
5
I don't like regex (just kidding I do but I'm not very good at it).
我不喜欢正则表达式(只是开玩笑,但我不是很擅长)。
String s = "asdf:1.0/1"
String text = s.substring(0,s.lastIndexOf(":"));
String doub = s.substring(s.lastIndexOf(":")+1,text.indexOf("/"));
String inte = s.substring(text.indexOf("/")+1,s.length());
#3
1
String.split()
is typically used in simpler scenarios where the delimiter and formatting are more consistent and when you don't know how many elements you are going to be splitting.
String.split()通常用于更简单的场景,其中分隔符和格式更一致,并且当您不知道要分割多少元素时。
Your use case calls for a plain old regular expression. You know the formatting of the string, and you know you want to collect three values. Try something like the following.
您的用例需要一个普通的旧正则表达式。您知道字符串的格式,并且您知道要收集三个值。尝试类似以下的内容。
Pattern p = Pattern.compile("(.+):([0-9\\.]+)/([0-9]+)$");
Matcher m = p.matcher(myString);
if (m.find()) {
String myText = m.group(1);
String myFloat = m.group(2);
String myInteger = m.group(3);
}