我有大字符串,例如:
String = "ABC114.553111.325785.658EFG114.256114.589115.898";
我想实现:
String1 = "ABC";
String2 = "114.553";
String3 = "111.325";
String4 = "785.658";
有时不是三位数字然后是点,然后是四位数字。但总是在数字之前有三个字母。
最佳答案
您可以使用以下正则表达式:
([^\.]){3}(\....)?
这里有一个程序...
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class C {
public static void main(String[] args) {
String a = "([^\\.]){3}(\\....)?";
String b = "ABC114.553111.325785.658EFG114.256114.589115.898";
Pattern pattern = Pattern.compile(a);
Matcher matcher = pattern.matcher(b);
while (matcher.find()) {
System.out.println("found: " + matcher.group(0));
}
}
}
输出
found: ABC
found: 114.553
found: 111.325
found: 785.658
found: EFG
found: 114.256
found: 114.589
found: 115.898
关于Java 在自定义位置拆分字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21758138/