Java 在自定义位置拆分字符串

标签 java string split

我有大字符串,例如:

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/

相关文章:

java - 两个日期之间的比较未显示正确的结果

java - 如果一个算法调用另一个算法来执行其功能,它的总体时间复杂度是否会受到影响?

python - “str”对象不可作为数组调用字符串

c - C语言的摩尔斯电码翻译器

C# 写入多个文件而无需不断关闭/重新打开流。流作家?

JavaFX 11 : IllegalAccessError when creating Label

java - 等到 Firebase 查询中的 onChildAdded 完成,Android

java - 以金字塔形式打印字符串

python - 使用 Python 将 JSON 文件分成相等/较小的部分

java - 在java中使用正则表达式和split方法时遇到问题