【问题标题】:Java REGEX: include new line and place results in an arrayJava REGEX:包含新行并将结果放入数组中
【发布时间】:2021-11-15 07:10:54
【问题描述】:

我有一个如下所示的原始文本:

#John
age: 25
skill: boxer

#Peter
age: 25
skill: fisher

#James
age: 25
skill: bouncer

我打算将每个块分开并放入一个数组中。

我的问题是如何使用正则表达式“获取所有以 '#' 开头并以 '#' 结尾的匹配文本。

我的目的是让我可以将 John 的块与 Peter 的块和 James 的块分开。

如果我使用这个:

String    regex = "#(.*)";
List<String> matches = Pattern.compile( regex, Pattern.MULTILINE)   
                    .matcher(raw)
                    .results()
                    .map(MatchResult::group)
                    .collect(Collectors.toList());

数组只包含:

index 0: #John
index 1: #Peter
index 2: #James

这是不完整的,因为它不包括身体的“年龄”和“技能”部分。 我想要的结果是这样的:

index 0: #John
         age: 25
         skill: boxer

index 1: #Peter
         age: 25
         skill: fisher

index 2: #James
         age: 25
         skill: bouncer

你能帮忙吗?

【问题讨论】:

    标签: java regex


    【解决方案1】:

    使用正式的正则表达式模式匹配器,我们可以尝试以下正则表达式查找所有方法:

    String input = "#John\nage: 25\nskill: boxer\n\n#Peter\nage: 25\nskill: fisher\n\n#James\nage: 25\nskill: bouncer";
    List<String> items = new ArrayList<>();
    String pattern = "(?s)(#.*?)\\s*(?=#|$)";
    Pattern r = Pattern.compile(pattern);
    Matcher m = r.matcher(input);
    int index = 0;
    while (m.find()) {
        items.add(m.group(1));
        System.out.println("index " + index++ + ": " + m.group(1));
    }
    

    打印出来:

    index 0: #John
    age: 25
    skill: boxer
    index 1: #Peter
    age: 25
    skill: fisher
    index 2: #James
    age: 25
    skill: bouncer
    

    使用的正则表达式模式表示匹配:

    (?s)             enable dot all mode, so dot matches across newlines
    (                capture what follows
    #                match a starting #
    .*?              then match all content until reaching the nearest
    )                end capture
    \\s*             optional whitespace
    (?=#|$)          followed by either the next # or end of the input
    

    【讨论】:

      【解决方案2】:

      您也可以在字符串的开头以# 开始匹配,并匹配以下所有不以# 开头的行

      ^#.*(?:\R(?!#).*)*
      

      模式匹配:

      • ^ 字符串开始
      • #.* 匹配 # 和该行的其余部分
      • (?:非捕获组
        • \R(?!#).* 匹配换行符并在开头断言不是 # 并匹配该行
      • )*关闭非捕获组并可选择重复

      Regex demo | Java demo

      String regex = "^#.*(?:\\R(?!#).*)*";
      String string = "#John\nage: 25\nskill: boxer\n\n#Peter\nage: 25\nskill: fisher\n\n#James\nage: 25\nskill: bouncer";
      
      Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
      Matcher matcher = pattern.matcher(string);
      List<String> strings = new ArrayList<>();
      
      while (matcher.find()) {
          strings.add(matcher.group(0));
      }
      
      for (String s : strings)
          System.out.println(s);
      

      输出

      #John
      age: 25
      skill: boxer
      
      #Peter
      age: 25
      skill: fisher
      
      #James
      age: 25
      skill: bouncer
      

      【讨论】:

      • 也感谢您的努力。我今天学到了很多。
      猜你喜欢
      • 1970-01-01
      • 2014-08-17
      • 2015-07-31
      • 1970-01-01
      • 2014-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多