【问题标题】:replaceFirst in a regex with $1 - JAVA用 $1 替换正则表达式中的第一个 - JAVA
【发布时间】:2021-03-27 17:45:04
【问题描述】:

我需要从一个字符串中提取以下模式,并根据输入返回一个可能的匹配项。

我使用了正则表达式,通过不同的可能性,我没有设法得到预期的结果:

输入 a): 71346 G249 USD 70045620 27/08/2020 001 / 004

输入 b): 71346 G249 USD 70045620/2020 27/08/2020 001 / 004

试一试

String result = data.replaceFirst ( "(.*?([0-9]{6,}\\/[0-9]{4}).*)|(.*?([0-9]{6,}).*)", "$1" );

试试两个

String result = data.replaceFirst ( ".*?([0-9]{6,}\\/[0-9]{4})|([0-9]{6,}).*", "$1" );

尝试三个

String result = data.replaceFirst ( ".*?([0-9]{6,})([0-9]{6,}\\/[0-9]{4}).*", "$1" );

根据输入的预期结果:

输入 a): 70045620

输入 b): 70045620/2020

【问题讨论】:

    标签: java regex string regexp-replace


    【解决方案1】:

    以这种方式使用交替捕获组将根据数据为您提供不同的组号。如果您希望在替换中使用单个组,则可以将第二部分设为可选。

    String[] strings = { 
        "71346 G249 USD 70045620 27/08/2020 001 / 004",
        "71346 G249 USD 70045620/2020 27/08/2020 001 / 004"
    };
    String regex = "^.*?\\b([0-9]{6,}(?:/[0-9]{4})?)\\b.*$";
    
    for (String s : strings) {
        System.out.println(s.replaceFirst(regex, "$1"));
    }
    

    查看Java demo

    输出

    70045620
    70045620/2020
    

    您也可以找到匹配项,而不是使用 replaceFirst。

    \b[0-9]{6,}(?:/[0-9]{4})?
    

    例如

    String[] strings = { 
        "71346 G249 USD 70045620 27/08/2020 001 / 004",
        "71346 G249 USD 70045620/2020 27/08/2020 001 / 004"
    };
    String regex = "\\b[0-9]{6,}(?:/[0-9]{4})?";    
    Pattern pattern = Pattern.compile(regex);    
    
    for (String s : strings) {
        Matcher matcher = pattern.matcher(s);
        if (matcher.find()) {
            System.out.println(matcher.group(0));
        }
    }
    

    查看另一个Java demo

    输出

    70045620
    70045620/2020
    

    【讨论】:

      【解决方案2】:

      我会在这里使用String#replaceAll 来解决这个问题:

      String[] inputs = { "71346 G249 USD 70045620 27/08/2020 001 / 004",
                          "71346 G249 USD 70045620/2020 27/08/2020 001 / 004" };
      for (String input : inputs) {
          String match = input.replaceAll(".*\\b(\\d{8}(?:/\\d{4})?)\\b.*", "$1");
          System.out.println(input + " => " + match);
      }
      

      打印出来:

      71346 G249 USD 70045620 27/08/2020 001 / 004 => 70045620
      71346 G249 USD 70045620/2020 27/08/2020 001 / 004 => 70045620/2020
      

      【讨论】:

        【解决方案3】:

        就我个人而言,我会避免为此使用正则表达式。 好像你只想要第四个词。像 string.split() 这样的东西可能会很好:

        import java.io.*; 
        
        public class HelloWorld{
        
             public static void main(String []args){
                String text = "71346 G249 USD 70045620 27/08/2020 001 / 004";
                String result = text.split(" ")[3];
                System.out.print(result); 
             }
        }
        

        上述程序将输出:70045620 用于第一个输入,70045620/2020 用于第二个输入。

        【讨论】:

          猜你喜欢
          • 2016-11-12
          • 2017-08-30
          • 2011-05-09
          • 2010-12-24
          • 2015-05-21
          • 2021-09-13
          • 1970-01-01
          • 2021-02-03
          • 1970-01-01
          相关资源
          最近更新 更多