正则表达式在java中的字符串中查找特定模式

萨加苏里

我需要在一个字符串中找到一个特定的模式并从中提取一个子字符串。例如字符串:

{'Opening Cost P&L per store','Opening Costs Capital per store','Average Monthly Revenue','GROSS MARGIN %'}] = N:DB('Store Cost', 
    !Country and Region, DB('New Store Plan', !Country and Region, 
    !ID numbers, !Budget version, 'Retailer Type'), ATTRS('New Stores', 
    !New Stores, '}Map_}Link_New Store Plan_3CStore Cost'), DB('New Store Plan', 
    !Country and Region, !ID numbers, !Budget version, 'Size'), 
    DB('New Store Plan', !Country and Region, !ID numbers, !Budget version, 
    'Franchise/Corporate'), 'DATA')

我必须搜索:
DB('only,而不是其他模式,例如S:DB('or DB('}

然后在找到此模式后,我必须在列表中获取文本,该列表在此模式之后可用,并且仅在引号中,例如

DB('Metrics cube-HumanResource',    !country, !Time, !gometric, !Metric Indicators), CONTINUE)​
DB('Metrics cube-InternalProcess',     !country, 'Total of Product', !Time, !gometric, !Metric Indicators), CONTINUE);​

那么输出将是:

1 - Metrics cube-HumanResource
2 - Metrics cube-InternalProcess​

这是我的代码。但它不打印任何东西:

public class StringRegex {
    public static void main(String[] args) {
        String str = "{'Opening Cost P&L per store','Opening Costs Capital per store','Average Monthly Revenue','GROSS MARGIN %'}] = N:DB('Store Cost', \n" +
                "    !Country and Region, DB('New Store Plan', !Country and Region, \n" +
                "    !ID numbers, !Budget version, 'Retailer Type'), ATTRS('New Stores', \n" +
                "    !New Stores, '}Map_}Link_New Store Plan_3CStore Cost'), DB('New Store Plan', \n" +
                "    !Country and Region, !ID numbers, !Budget version, 'Size'), \n" +
                "    DB('New Store Plan', !Country and Region, !ID numbers, !Budget version, \n" +
                "    'Franchise/Corporate'), 'DATA')";
        String[] strArray = str.split(",");
        for(String s : strArray){
            if(s.matches("DB\\('.+'")){
                System.out.println(s);
            }
        }
    }
}
轴心

我会使用 aPattern和 aMatcher而不是拆分String. 你会更容易得到结果。使用以下正则表达式:

.*?[^:]DB\((.*?)\).*?

这将捕获每个 DB(.*) 的内容,而不是以:. 使用惰性量词*?将防止出现问题,例如在右括号之前捕获的不仅仅是文本。

Regex101

    String regex = ".*?[^:]DB\\((.*?)\\).*?";
    String string = "{'Opening Cost P&L per store','Opening Costs Capital per store','Average Monthly Revenue','GROSS MARGIN %'}] = N:DB('Store Cost', \n"
            + "    !Country and Region, DB('New Store Plan', !Country and Region, \n"
            + "    !ID numbers, !Budget version, 'Retailer Type'), ATTRS('New Stores', \n"
            + "    !New Stores, '}Map_}Link_New Store Plan_3CStore Cost'), DB('New Store Plan', \n"
            + "    !Country and Region, !ID numbers, !Budget version, 'Size'), \n"
            + "    DB('New Store Plan', !Country and Region, !ID numbers, !Budget version, \n" + "    'Franchise/Corporate'), 'DATA')";

    Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
    Matcher matcher = pattern.matcher(string);

    while (matcher.find()) {
        System.out.println("> " + matcher.group(1));
    }

结果 :

> 'New Store Plan', !Country and Region, 
    !ID numbers, !Budget version, 'Retailer Type'  
> 'New Store Plan', 
    !Country and Region, !ID numbers, !Budget version, 'Size'  
> 'New Store Plan', !Country and Region, !ID numbers, !Budget version, 
    'Franchise/Corporate'  

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

正则表达式,用于在字符串中查找特定模式

来自分类Dev

正则表达式可在Java字符串中查找特定单词

来自分类Dev

使用python正则表达式在有限字符字符串中查找特定模式

来自分类Dev

查找所有字符串的正则表达式是在Java中以$开头的正则表达式

来自分类Dev

用于从给定字符串中查找多个模式的正则表达式

来自分类Dev

正则表达式-在字符串中查找日期模式

来自分类Dev

PHP:使用正则表达式查找字符串中的数组模式

来自分类Dev

使用正则表达式和python 3在字符串中查找模式

来自分类Dev

正则表达式-在特定字符之前的字符串中查找各种数字

来自分类Dev

在正则表达式中搜索模式时如何忽略特定的字符串?

来自分类Dev

在正则表达式中搜索模式时如何忽略特定的字符串?

来自分类Dev

使用正则表达式在URL中查找特定的字符串

来自分类Dev

Java中的正则表达式查找字符串中的冒号

来自分类Dev

字符串中的Java正则表达式

来自分类Dev

Java中的正则表达式从给定的字符串中找到类似%的模式

来自分类Dev

字符串模式与Java中的正则表达式匹配

来自分类Dev

使用正则表达式在Java中查找子字符串

来自分类Dev

需要正则表达式来查找特定模式的所有子字符串

来自分类Dev

查找具有特定模式但未在列表中找到的字符串(Java正则表达式)

来自分类Dev

字符串模式的 Java 正则表达式

来自分类Dev

正则表达式:在不同的字符串变体中查找字符串

来自分类Dev

Java中特定字符串的正则表达式

来自分类Dev

正则表达式在R中查找特定模式

来自分类Dev

正则表达式在python中查找特定模式

来自分类Dev

正则表达式模式可在字符串的每一行中查找整数

来自分类Dev

在使用C#使用正则表达式查找字符串模式中需要帮助

来自分类Dev

如何使用正则表达式从字符串中删除特定字符串

来自分类Dev

如何获得在Java中的正则表达式模式字符串列表和匹配字符串

来自分类Dev

正则表达式查找字符串中是否存在奇数个特定字符

Related 相关文章

  1. 1

    正则表达式,用于在字符串中查找特定模式

  2. 2

    正则表达式可在Java字符串中查找特定单词

  3. 3

    使用python正则表达式在有限字符字符串中查找特定模式

  4. 4

    查找所有字符串的正则表达式是在Java中以$开头的正则表达式

  5. 5

    用于从给定字符串中查找多个模式的正则表达式

  6. 6

    正则表达式-在字符串中查找日期模式

  7. 7

    PHP:使用正则表达式查找字符串中的数组模式

  8. 8

    使用正则表达式和python 3在字符串中查找模式

  9. 9

    正则表达式-在特定字符之前的字符串中查找各种数字

  10. 10

    在正则表达式中搜索模式时如何忽略特定的字符串?

  11. 11

    在正则表达式中搜索模式时如何忽略特定的字符串?

  12. 12

    使用正则表达式在URL中查找特定的字符串

  13. 13

    Java中的正则表达式查找字符串中的冒号

  14. 14

    字符串中的Java正则表达式

  15. 15

    Java中的正则表达式从给定的字符串中找到类似%的模式

  16. 16

    字符串模式与Java中的正则表达式匹配

  17. 17

    使用正则表达式在Java中查找子字符串

  18. 18

    需要正则表达式来查找特定模式的所有子字符串

  19. 19

    查找具有特定模式但未在列表中找到的字符串(Java正则表达式)

  20. 20

    字符串模式的 Java 正则表达式

  21. 21

    正则表达式:在不同的字符串变体中查找字符串

  22. 22

    Java中特定字符串的正则表达式

  23. 23

    正则表达式在R中查找特定模式

  24. 24

    正则表达式在python中查找特定模式

  25. 25

    正则表达式模式可在字符串的每一行中查找整数

  26. 26

    在使用C#使用正则表达式查找字符串模式中需要帮助

  27. 27

    如何使用正则表达式从字符串中删除特定字符串

  28. 28

    如何获得在Java中的正则表达式模式字符串列表和匹配字符串

  29. 29

    正则表达式查找字符串中是否存在奇数个特定字符

热门标签

归档