我的测试用文件this is test-text content:
1.<img src="../a.gif">
2.<img src="a.gif">
3.<link href="../a.gif">
4.<link href="a.gif">
5.<link href="http://www.a.b/c.gif">
6.<a href="#a.htm">
5.<a href="http://www.a.b/c.gif">
7.<a href="mailto:[email protected]">谢谢各位了
1.<img src="../a.gif">
2.<img src="a.gif">
3.<link href="../a.gif">
4.<link href="a.gif">
5.<link href="http://www.a.b/c.gif">
6.<a href="#a.htm">
5.<a href="http://www.a.b/c.gif">
7.<a href="mailto:[email protected]">谢谢各位了
解决方案 »
- 如何安装JAVA
- 跪求将jar文件生成exe文件方法
- 请教怎么找出数组中出现次数最多的元素
- 关于JTable(Swing桌面开发)如何定位列的问题
- 请教:Vista U +IE8,安装了j6u9,发现本机html里的脚本在IE无反应,远程的东东或ff却好好的
- java怎么实现richtextbox 用.net做不出来这东西
- String s=new String("avc")共生成几个对象?
- 把C语言程序改成JAVA程序时,遇到指针怎么办?
- 新年,我对大家有关MS,SUN的一个要求!
- 下午考试JAVA,SCJP形式,全英文(变X),玩完了,惨了,痛苦,无处发泄,散分。。
- JDK在XP下的配置????
- 窗体中有三个并列放置JTextArea, 当把窗体最大化时, 怎样让它们也按照一个特定的比例缩放?
regex = "[\\w\\W]*(\\s*[h|H][R|r][E|e][F|f]\\s*=\\s*\"\\s*\\.\\s*#\\s*[(http)(mailto)])[\\w|\\W]*"
可以屏蔽掉:.#http开头的和.#mailto开头的如果也要屏蔽掉 ".#http mailto的话表达式如下:
regex1 = "[\\w\\W]*(\\s*[h|H][R|r][E|e][F|f]\\s*=\\s*\"\\s*\\.\\s*#\\s*[(http)(mailto)((http)\\s*(mailto))])[\\w|\\W]*";
不知道能否完成你要的功能.
import java.util.regex.Matcher;public class href {
public static void main(String[] args) {
Pattern p = Pattern.compile(".+href=\"(?!(\\.|#|http:|mailto:)).+",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("");
String[] example = {
"<img src=\"../a.gif\">",
"<img src=\"a.gif\">",
"<link href=\"../a.gif\">",
"<link href=\"a.gif\">",
"<link href=\"http://www.a.b/c.gif\">",
"<a href=\"#a.htm\">",
"<a href=\"http://www.a.b/c.gif\">",
"<a href=\"mailto:[email protected]\">", }; for (int i = 0; i < example.length; i++) {
m.reset(example[i]);
if (m.matches()) {
System.out.println(m.group());
}
}
}
}如此即可
我可能让你误会了,我是指屏蔽以 . # http mailto 中任一个开头的href,不是这些一起开头;你看我的test用例就知道是什么意思了。to:registered(已注册)
我不能使用?!,因为它匹配 pattern 但不获取匹配结果,我需要得到获取匹配结果;另外,我也不能使用诸如if (m.matches()) 的判断语句,因为我使用replaceAll()做文档替换。好像没有判断后做单一替换的方法吧(只有replaceFirst和replaceAll),你觉得怎么解决?
this is test-text content:
1.<img src="../a.gif">
2.<img src="a.gif">
3.<link href="../a.gif">
4.<link href="a.gif">
5.<link href="http://www.a.b/c.gif">
6.<a href="#a.htm">
7.<a href="http://www.a.b/c.gif">
8.<a href="mailto:[email protected]">
我想替换第4条的为全路径d:/test/a.gif,需求就是:
有一个文件流,我要把里面的相对路径全部替换为绝对路径,当然,../ http mailto 等不能替换(../在后一步操作即可)
一个用来过滤, 另一个用来替换
而且批量处理不要用 String 的 replace, 开销太大public class href {
public static void main(String[] args) {
Pattern p = Pattern.compile(".+href=\"(?!(\\.|#|http:|mailto:)).+",
Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher("");
Pattern p2 = Pattern.compile("\".+\"");
Matcher m2 = p2.matcher("");
StringBuffer buff; String[] example = {
"<img src=\"../a.gif\">",
"<img src=\"a.gif\">",
"<link href=\"../a.gif\">",
"<link href=\"a.gif\">",
"<link href=\"http://www.a.b/c.gif\">",
"<a href=\"#a.htm\">",
"<a href=\"http://www.a.b/c.gif\">",
"<a href=\"mailto:[email protected]\">", };
String replacement = "\"d:/test/a.gif\""; for (int i = 0; i < example.length; i++) {
m.reset(example[i]);
m2.reset(example[i]);
if (m.matches()) {
buff = new StringBuffer();
while (m2.find()) {
m2.appendReplacement(buff, replacement);
}
m2.appendTail(buff);
System.out.println(buff.toString());
}
}
}
}
首先,我有些东西没说明白:
1 我的test内容只是为方便给大家帮我测试,才写的很工整,我的输入实际中是任意文件输入流,即是html文件流。所以不能是工整的数组循环,也因此,应该replacement=d:/test+文件名;
2 由于上,我觉得判断语句if (m.matches()) while (m2.find()) 我都用不上,因为我用replaceAll(),如果不用replaceAll(),我就不知道用什么了,
3 ?!并不返回匹配,而我的文件名是必须靠group(n)来动态获得的;
你说呢?
public static void call()
{
try
{
String sUrl = "";
File file = new File("test.txt");
URL url = file.toURL();
StringBuffer sb = new StringBuffer();
InputStream in = url.openStream();
InputStream input = new BufferedInputStream(in);
Reader reader = new InputStreamReader(input);
for (int c = reader.read(); c >= 0; c = reader.read())
{
sb.append((char) c);
}
String sInput = sb.toString(); System.out.println(sInput); String sPattern = ""; String srcPath = "D:/Test/";
sPattern = "\\s[Hh][Rr][Ee][Ff]\\s*=\\s*\"([^\\.#(http)]+[^\"]*)\""; String sOutput = matchFilePath(sInput, sPattern, srcPath, "href"); System.out.println(sOutput);
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static String matchFilePath(String sInput, String sPattern,
String replacement, String attrName)
{
String outer = null; try
{
Pattern pattern = Pattern.compile(sPattern,
Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(sInput); //System.out.println(matcher.groupCount()); while (matcher.find())
{
String sMatch = matcher.group(1);
System.out.println("sMatch = " + sMatch);
} outer = matcher.replaceAll(" " + attrName + "=\"" + replacement
+ "$1\""); } catch (PatternSyntaxException e)
{
e.printStackTrace();
} return outer;
}
import java.util.regex.Matcher;public class ReplaceHref {
public static String replaceHref(String html, String srcPath) {
Pattern p1 = Pattern.compile(
"<.*href=\"(?!(\\.|#|http:|mailto:)).*\">",
Pattern.CASE_INSENSITIVE);
Pattern p2 = Pattern.compile("\"(.*)\"");
Matcher m1 = p1.matcher(html);
Matcher m2 = p2.matcher("");
StringBuffer buff = new StringBuffer();
String blank = ""; while (m1.find()) {
m1.appendReplacement(buff, blank);
m2.reset(m1.group(0));
while (m2.find()) {
m2.appendReplacement(buff,
"\"" + srcPath + "$1" + "\"");
}
m2.appendTail(buff);
}
m1.appendTail(buff); return buff.toString();
} public static void main(String[] args) {
String example = "1.<img src=\"../a.gif\">\n" +
"2.<img src=\"a.gif\">\n" +
"3.<link href=\"../a.gif\">\n" +
"4.<link href=\"a.gif\">\n" +
"5.<link href=\"http://www.a.b/c.gif\">\n" +
"6.<a href=\"#a.htm\">\n" +
"5.<a href=\"http://www.a.b/c.gif\">\n" +
"7.<a href=\"mailto:[email protected]\">";
String path = "C:\\\\Windows\\\\"; System.out.println(replaceHref(example, path));
}
}
我觉得你的Pattern2/matcher2设置的真是妙啊!呵呵
(不过,regex表达式有点小问题,我在正式的测试中居然可以匹配align="right",然后该匹配的没匹配,我做了改动就好了,当然还是用你的推崇的“?!”)