Pattern p = Pattern.compile("<(a[^>]*)>"); Matcher m = p.matcher(str); while (m.find()) { int start = m.start(); int end = m.end(); chars[start] = '['; chars[end-1] = ']'; } String result = new String(chars); System.out.println("before:" + str); System.out.println(" after:" + result);
} }
不好使?怎么可能public class Test { public static void main(String[] args) { System.out.println("cc<a href='a.htm'>abc<br>ee".replaceAll("<(a[^>]*)>","[$1]")); } }
s=s.replaceAll(">","]");
char[] chars = str.toCharArray();
Pattern p = Pattern.compile("<(a[^>]*)>");
Matcher m = p.matcher(str);
while (m.find())
{
int start = m.start();
int end = m.end();
chars[start] = '[';
chars[end-1] = ']';
}
String result = new String(chars);
System.out.println("before:" + str);
System.out.println(" after:" + result);
}
}
public static void main(String[] args) {
System.out.println("cc<a href='a.htm'>abc<br>ee".replaceAll("<(a[^>]*)>","[$1]"));
}
}