java实现php中preg_split 函数
改函数可用于提取文章部分内容作为摘要。
java 实现php中preg_split 函数 /**
* 利用正则表达式分隔字符串
* @param pattern 用于分隔的正则表达式
* @param input 要分隔的字符串
* @param limit 限制输出匹配串个数,小于等于0则不限制
* @param delimCapture 是否输出表达式匹配的内容。
* @return
*/
public static String[] split(String pattern,CharSequence input, int limit,boolean delimCapture) {
if(input ==null)
return null;
if(pattern == null)
return new String[] {input.toString()};
int index = 0;
boolean matchLimited = limit > 0;
ArrayList<String> matchList = new ArrayList<String>();
Matcher m = Pattern.compile(pattern).matcher(input);
// Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
String match = input.subSequence(index, m.start()).toString();
if(!StringUtils.isEmpty(match))
matchList.add(match);
index = m.end();
} else if (matchList.size() == limit - 1) { // last one
String match = input.subSequence(index,input.length()).toString();
if(!StringUtils.isEmpty(match))
matchList.add(match);
index = m.end();
}
if(delimCapture){
System.out.println(m.groupCount());
for(int i=1;i<=m.groupCount();i++)
matchList.add(m.group(i));
}
}
// If no match was found, return this
if (index == 0)
return new String[] {input.toString()};
// Add remaining segment
if (!matchLimited || matchList.size() < limit)
matchList.add(input.subSequence(index, input.length()).toString());
// Construct result
int resultSize = matchList.size();
if (limit == 0)
while (resultSize > 0 && "".equals(matchList.get(resultSize-1)))
resultSize--;
String[] result = new String[resultSize];
return matchList.subList(0, resultSize).toArray(result);
}
java 实现php中preg_split 函数 /**
* 利用正则表达式分隔字符串
* @param pattern 用于分隔的正则表达式
* @param input 要分隔的字符串
* @param limit 限制输出匹配串个数,小于等于0则不限制
* @param delimCapture 是否输出表达式匹配的内容。
* @return
*/
public static String[] split(String pattern,CharSequence input, int limit,boolean delimCapture) {
if(input ==null)
return null;
if(pattern == null)
return new String[] {input.toString()};
int index = 0;
boolean matchLimited = limit > 0;
ArrayList<String> matchList = new ArrayList<String>();
Matcher m = Pattern.compile(pattern).matcher(input);
// Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
String match = input.subSequence(index, m.start()).toString();
if(!StringUtils.isEmpty(match))
matchList.add(match);
index = m.end();
} else if (matchList.size() == limit - 1) { // last one
String match = input.subSequence(index,input.length()).toString();
if(!StringUtils.isEmpty(match))
matchList.add(match);
index = m.end();
}
if(delimCapture){
System.out.println(m.groupCount());
for(int i=1;i<=m.groupCount();i++)
matchList.add(m.group(i));
}
}
// If no match was found, return this
if (index == 0)
return new String[] {input.toString()};
// Add remaining segment
if (!matchLimited || matchList.size() < limit)
matchList.add(input.subSequence(index, input.length()).toString());
// Construct result
int resultSize = matchList.size();
if (limit == 0)
while (resultSize > 0 && "".equals(matchList.get(resultSize-1)))
resultSize--;
String[] result = new String[resultSize];
return matchList.subList(0, resultSize).toArray(result);
}