簡體   English   中英

正則表達式以在Java字符串中查找特定模式

[英]regex to find a particular pattern in a string in 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(' ,而不是S:DB('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);
            }
        }
    }
}

我會使用PatternMatcher而不是拆分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'  

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM