【问题标题】:Regular Expression for string in javajava中字符串的正则表达式
【发布时间】:2023-03-22 12:30:02
【问题描述】:

我正在尝试为这些字符串查找编写正则表达式

05 IMA-POLICY-ID         PIC X(15).               00020068

05 (AMENT)-GROUPCD       PIC X(10).

我想解析 05 和 first tab 之间的任何内容。 该行可能以制表符或空格开头,然后是数字 初始数字可以是任何 05,10,15 。

所以在第一行我需要传递 IMA-POLICY-ID 和第二行 (AMENT)-GROUPCD

这是我写的代码,它没有找到我哪里出错的模式?

Pattern p1 = Pattern.compile("^[0-9]+\\s\\S+\t$"); 
Matcher m1 = p1.matcher(line); 
System.out.println("m1 =="+m1.group());

【问题讨论】:

  • 并移除锚点。
  • @devnull: \t 也应该可以工作(它匹配文字制表符而不是制表符元字符,但效果相同)。
  • 嗨,该行可能以制表符或空格开头,然后是数字

标签: java regex pattern-matching


【解决方案1】:

您的模式期望该行在IMA-POLICY-ID 等之后结束,因为末尾有$。

如果您要匹配的字符串中没有空格(我假设不是因为您使用了\S+,我会将模式更改为^\d+\s+(\S+),这应该足以匹配任何数字在行首,然后是空格,然后是要匹配的非空格字符组(请注意,制表符也是空格)。

如果您需要匹配到第一个制表符或输入的末尾并包含其他空格,请将(\S+) 替换为([^\t]+)。

【讨论】:

    【解决方案2】:
    Pattern p1 = Pattern.compile("\\b(?:05|1[05])\\b[^\\t]*\\t"); 
    

    将匹配从05、10 或15 到最近的\t 的任何内容。

    说明:

    \b           # Start of number/word
    (?:05|1[05]) # Match 05, 10 or 15
    \b           # End of number/word
    [^\t]*       # Match any number of characters except tab
    \t           # Match a tab
    

    【讨论】:

      【解决方案3】:
      ^\d+\s+([^\s]+)
      

      这将符合您的要求

      在这里演示:http://regex101.com/r/rQ7fT3

      【讨论】:

        【解决方案4】:

        我可以看到两件事可能会阻止您的 Pattern 工作。

        1. 首先,您的输入Strings 包含多个制表符分隔值,因此Pattern 末尾的$“输入结束”字符将无法匹配String
        2. 其次,您要查找05(等)和第一个选项卡之间的内容。因此,您需要将所需的表达式用括号括起来(例如(\\S+))并通过其组号引用它(在这种情况下,它将是组1)

        这是一个例子:

        String input = "05 IMA-POLICY-ID\tPIC X(15).\t00020068" +
                        "\r\n05 (AMENT)-GROUPCD\tPIC X(10).";
        //                           | 0, 1, or 5 twice (refine here if needed)
        //                           |       | 1 whitespace
        //                           |       |  | your queried expression (here I use a 
        //                           |       |  | reluctant dot search
        //                           |       |  |    | tab
        //                           |       |  |    |  | anything after, reluctant
        Pattern p = Pattern.compile("[015]{2}\\s(.+?)\t.+?");
        Matcher m = p.matcher(input);
        while (m.find()) {
            System.out.println("Found: " + m.group(1));
        }
        

        输出

        Found: IMA-POLICY-ID
        Found: (AMENT)-GROUPCD
        

        【讨论】:

          【解决方案5】:

          您的正则表达式几乎是正确的。只需删除正则表达式末尾的 \t$ 即可。并将\\S+ 捕获为一个组。

          Pattern p1 = Pattern.compile("^[0-9]+\\s(\\S+)");
          

          现在打印为:

          if (m.find( )) {
              System.out.println(m.group(1));
          }
          

          【讨论】:

            【解决方案6】:

            这就是我想出的,它奏效了:

            String re = "^\\s+\\d+\\s+([^\\s]+)";
            Pattern p1 = Pattern.compile(re, Pattern.MULTILINE); 
            Matcher m1 = p1.matcher(line);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2023-04-08
              相关资源
              最近更新 更多