获取字符串包含括号的区间集合

获取字符串包含括号的区间集合
 
一。目的:获取字符串包含括号的List<String> 集合
 
如:

 

 
String roads = "G6包头..。?九原收费站((这个是.。--.。?测试)——210国道——幸福南路(备注:车、货、路线不变,往返)——210国道(((aa)——210国道(这样呢)";
 
二。代码
 
 

 //中英文括号以及括号里面内容正则匹配
 public static final String DJYS_STD_FEATURE_REG_BRACKETS = "\\(.*?\\)|\\(.*?\\)";
 
/**
* 获取通行路线包含括号的区间集合
* 如:List<String> 里的值 [3,9] [15,23]
*/
private List getBracketsIntervalList(String str){
Matcher matcher = Pattern.compile(FeatureConst.DJYS_STD_FEATURE_REG_BRACKETS).matcher(str.replaceAll(" +",""));
List list = new ArrayList<>();
//一。获取括号的区间范围
while(matcher.find()){
//matcher.start()匹配的开始位置(从0开始)
int start = matcher.start();
//matcher.end()匹配的结束位置+1(从0开始)
int end = matcher.end()-1;
//获取包含括号的闭区间范围
String interval = "[" + start + "," + end + "]";
list.add(interval);
}
return list;
}

 

三。结果
 
posted @ 2019-08-21 11:10  ejQiu  阅读(439)  评论(0编辑  收藏  举报