[英]regex to find a particular pattern in a string in java
I need to find a particular pattern in a string and extract a substring out of it. 我需要在字符串中找到特定的模式,然后从中提取一个子字符串。 Eg of the String:
例如字符串:
{'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')
I have to search for: 我必须搜索:
DB('
only, not other pattern like S:DB('
or DB('}
. 仅限于
DB('
,而不是S:DB('
或DB('}
等其他模式。
Then after finding this pattern I have to take text in list which is available after this pattern and in quotes only eg 然后找到该模式后,我必须将文本放在列表中,该列表在此模式之后可用,并且仅用引号
DB('Metrics cube-HumanResource', !country, !Time, !gometric, !Metric Indicators), CONTINUE)
DB('Metrics cube-InternalProcess', !country, 'Total of Product', !Time, !gometric, !Metric Indicators), CONTINUE);
then output will be: 那么输出将是:
1 - Metrics cube-HumanResource
2 - Metrics cube-InternalProcess
This is my code. 这是我的代码。 But its not printing anything:
但它不打印任何内容:
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);
}
}
}
}
I would go with a Pattern
and a Matcher
instead of splitting the String
. 我会使用
Pattern
和Matcher
而不是拆分String
。 You will get a result easier. 您将更容易获得结果。 Using the following regex :
使用以下正则表达式:
.*?[^:]DB\((.*?)\).*?
This will capture the content of every DB(.*) not precede with a :
. 这将捕获每个不以
:
开头的DB(。*)的内容。 Using lazy quantifier *?
使用惰性量词
*?
will prevent problem like capturing more than just the text until the closing parenthesis. 可以避免捕获诸如仅在结束括号之前的文本之类的问题。
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));
}
Results : 结果:
> '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.