public static String ReturnBetween(String heap, String startEx, String endEx, boolean include) {
int startPos = 0;
int endPos = heap.length();
String starts = "";
String ends = "";
if (!startEx.equals("^")) {
Pattern regexStart = Pattern.compile(startEx, Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
starts = regexStart.matcher(heap).toString();
if (starts.equals("")) {
startPos = -1;
} else {
startPos = heap.indexOf(starts);
}
}
if (startPos == -1) {
return "";
}
if (!endEx.equals("$")) {
Pattern regexEnd = Pattern.compile(endEx, Pattern.CASE_INSENSITIVE | Pattern.DOTALL );
ends = regexEnd.Match(heap, startPos + starts.length()).toString();
if (ends.equals("")) {
endPos = -1;
} else {
endPos = heap.indexOf(ends, startPos + starts.length());
}
}
if (endPos == -1) {
return "";
}
if (!include) {
startPos += starts.length();
}
if (include) {
endPos += ends.length();
}
String result = heap.substring(startPos, endPos);
return result;
}
这是一个C函数,用于获取两个变量之间的字符串,我试图把它转换成Java函数,大部分已经被转换成Java代码。
我已经成功地转换了这个函数。除本部分外:
ends = regexEnd.Match(heap, startPos + starts.length()).toString();