用java语言怎样实现如下过滤条件
本人初学java,对于java正则表达式不是很理解,我想用java实现下面条件的字符串过滤,还请各位大侠帮助。
text = X+..+A + B + C +..Y,这里,text是原字符串,B是需要过滤出来的字符串,A和C是过滤B的条件,X+..和..Y是其它的干扰字符串。
本人描述可能很不清晰,举个例来说吧:
public static void main(String[] args) {
String text = "the background="yello",good color";
String A = "background="";
String C = """;
String B = getParam(text, A, C);
System.out.println("B : " + B);
}
public static String getParam(String src, String findLeft, String findRight) {
int indexFirst = -1;
int indexSecond = -1;
indexFirst = index(src, findLeft, 0);
if (indexFirst >= 0) {
indexSecond = index(src, findRight, indexFirst + findLeft.length());
if (indexSecond >= 0) {
return src.substring(indexFirst + findLeft.length(), indexSecond);
}
}
return null;
}
public static int index(String src, String obj, int indexBegin) {
int indexRet = -1;
for (int i = indexBegin, j = 0; i < src.length(); i++) {
if (src.charAt(i) == obj.charAt(0)) {
while(src.charAt(j + i) == obj.charAt(j++)) {
if (j >= obj.length()) {
indexRet = i;
break;
}
if (j + i >= src.length()) {
return -1;
}
}
}
}
return indexRet;
}