有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java正则表达式,用于在缺少分隔符的字符之间添加数字

我没有经常使用正则表达式,我需要一点帮助。我有一种情况,我的数字是用点字符分隔的,比如:

0.0.1
1.1.12.1
20.3.4.00.1

现在我想确保.之间的每个数字都有两位数:

00.00.01
01.01.12.01
20.03.04.00.01

我怎样才能做到这一点?谢谢你的帮助


共 (5) 个答案

  1. # 1 楼答案

    我假设你想确保每个整数至少是两位数字,都在.和两端。这就是我想到的

    public String ensureTwoDigits(String original){
        return original.replaceAll("(?<!\\d)(\\d)(?!\\d)","0$1");
    }
    

    测试用例

    public static void main(String[] args) {
        Foo f = new Foo();
        List<String> values = Arrays.asList("1",
              "1.1",
              "01.1",
              "01.01.1.1",
              "01.2.01",
              "01.01.01");
        values.forEach(s -> System.out.println(s + " -> " + f.ensureTwoDigits(s)));
    }
    

    测试输出

    1 -> 01
    1.1 -> 01.01
    01.1 -> 01.01
    01.01.1.1 -> 01.01.01.01
    01.2.01 -> 01.02.01
    01.01.01 -> 01.01.01
    

    regex(?<!\\d)(\\d)(?!\\d)同时使用负向后看和负向前看来检查一个数字周围是否有其他数字。否则,它会在每一个数字前加一个零。替换字符串"0$1"表示在第一个捕获组前面放置一个0。实际上只有一个,那就是(\\d)——一位数的发生率

    编辑:我应该注意到,我意识到这与最初的要求并不完全相符。在一位数之间使用什么并不重要——字母、各种标点符号等都会返回,任何一个位数前面都是零。如果希望它失败或跳过可能包含数字和.以外的字符的字符串,则需要更改正则表达式

  2. # 2 楼答案

    您可以迭代通过匹配以下表达式检索到的匹配组:/([^.]+)/g

    例如:

    public class StackOverFlow {
    
        public static String text;
        public static String pattern;
    
        static {
            text = "20.3.4.00.1";
            pattern = "([^.]+)";
        }
    
        public static String appendLeadingZero(String text) {
            Pattern p = Pattern.compile(pattern);
            Matcher m = p.matcher(text);
    
            StringBuilder sb = new StringBuilder();
            while (m.find()) {
                String firstMatchingGroup = m.group(1);
    
                if (firstMatchingGroup.length() < 2) {
                    sb.append("0" + firstMatchingGroup);
                } else {
                    sb.append(firstMatchingGroup);
                }
    
                sb.append(".");
            }
    
            return sb.substring(0, sb.length() - 1);
        }
    
        public static void main(String[] args) {
            System.out.println(appendLeadingZero(text));
        }   
    }
    
  3. # 3 楼答案

    使用这种模式

    \b(?=\d(?:\.|$))
    

    并替换为0

    Demo

    \b              # <word boundary>
    (?=             # Look-Ahead
      \d            # <digit 0-9>
      (?:           # Non Capturing Group
        \.          # "."
        |           # OR
        $           # End of string/line
      )             # End of Non Capturing Group
    )               # End of Look-Ahead
    
  4. # 4 楼答案

    您可以使用以下简单的正则表达式:

    \\b\\d\\b
    

    并替换为0$0

  5. # 5 楼答案

    您可以使用String.split()来实现这一点:

    public static void main(String[] args) {
        String[] splitString = "20.3.4.00.1".split("\\.");
        String output = "";
        for(String a : splitString)
        {
            if(a.length() < 2)
            {
                a = "0" + a;
            }
            output += a + ".";
        }
        output = output.substring(0, output.length() - 1);
        System.out.println(output);
    }