我需要在字符串中找到特定的模式并从中提取子字符串。 字符串的例子:
{'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);
}
}
}
}
答案 0 :(得分:2)
我会选择Pattern
和Matcher
,而不是分割String
。您将获得更轻松的结果。使用以下正则表达式:
.*?[^:]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'