为什么不使用这个java程序?

为什么不使用这个java程序?,java,regex,Java,Regex,我想从程序中获取文件。 如果我只尝试使用“p”或“p2”,则有效。但这两种模式都没有 import java.util.regex.Pattern; import java.util.regex.Matcher; import java.io.*; public class imageurl { public static void main(String[] args) throws IOException { for ( int i = 1

我想从程序中获取文件。 如果我只尝试使用“p”或“p2”,则有效。但这两种模式都没有

import java.util.regex.Pattern;
import java.util.regex.Matcher;
import java.io.*;

public class imageurl
{
    public static void main(String[] args) 
            throws IOException
    {
    for ( int i = 1; i < 5000; i++ )
        {
        toContent(i);
        Pattern p = Pattern.compile("cacheimages/(.*)[\"][ ]*target=");
        Pattern p2 = Pattern.compile("</b>[ ]*[(](.*)[)]</div>");
        BufferedReader r = new BufferedReader(new FileReader("source\\source"+i+".txt"));
        String line;
        FileWriter writer = new FileWriter("imageurl\\imageurl"+i+".txt");
        while ((line = r.readLine()) != null )
            {
            Matcher m = p.matcher(line);
            Matcher m2 = p2.matcher(line);
            while (m.find())
            while (m2.find())
                {
                String c = (m.group(1));
                String c2 = (m2.group(1));
                System.out.println("<name>"+c2+"</name>_<url>http://www.geocaching.hu/cacheimages/"+c+"</url>"+"\n");
                writer.write("<name>"+c2+"</name>_<url>http://www.geocaching.hu/cacheimages/"+c+"</url>"+"\n");
                }
            }
            writer.close();
        }
    }
    private static void toContent(int i)
    {
    }
}
import java.util.regex.Pattern;
导入java.util.regex.Matcher;
导入java.io.*;
公共类imageurl
{
公共静态void main(字符串[]args)
抛出IOException
{
对于(int i=1;i<5000;i++)
{
内容(一);
Pattern p=Pattern.compile(“cacheimages/(.*)[\“][]*target=”);
Pattern p2=Pattern.compile(“[]*[(](.*)[)]”);
BufferedReader r=新的BufferedReader(新文件读取器(“source\\source”+i+“.txt”);
弦线;
FileWriter writer=newfilewriter(“imageurl\\imageurl”+i+“.txt”);
而((line=r.readLine())!=null)
{
匹配器m=p.匹配器(线);
匹配器m2=p2.匹配器(线);
while(m.find())
while(m2.find())
{
字符串c=(m.group(1));
字符串c2=(m2.组(1));
系统输出打印项次(“+c2+”_http://www.geocaching.hu/cacheimages/“+c+”+“\n”);
writer.write(“+c2+”_http://www.geocaching.hu/cacheimages/“+c+”+“\n”);
}
}
writer.close();
}
}
私有静态void到内容(int i)
{
}
}

如果每
m.find()
有一个
m2.find()
则如果只有一个
while
和一个
If
而不是第二个
while
,就可以了


否则,如果一个
m.find()
有更多的
m2.find()
,则必须检查
m2.find()
的位置是否在两个
m.find()
之间(正确的位置和下一个位置).

问题在于,您匹配的两个表达式并不同时存在于同一行中。您需要同时读取两行以获得所需的结果

. . .
String line2;
while ((line = r.readLine()) != null )
    Matcher m=p.matcher(line);
    if (m.find()) {
        if (line2 = r.readLine() != null) {
            Matcher m2=p2.matcher(line);
            if (m2.find()) {
                String c=m.group(1);
                String c2=m2.group(1);
                String outmsg=String.format("<name>%s</name>_<url>http://www.geocaching.hu/cacheimages/%s</url>\n", c2, c);
                System.out.print(outmsg);
                writer.write(outmsg);
            }
        }
        writer.close();
    }
}
。
弦线2;
而((line=r.readLine())!=null)
匹配器m=p.匹配器(线);
if(m.find()){
如果(line2=r.readLine()!=null){
匹配器m2=p2.匹配器(线);
if(m2.find()){
字符串c=m.group(1);
字符串c2=m2。组(1);
String outmsg=String.format(“%s_http://www.geocaching.hu/cacheimages/%s\n“,c2,c);
系统输出打印(输出消息);
writer.write(outmsg);
}
}
writer.close();
}
}

您不能简单地复制每一行。您无意中嵌套了两个
while
循环:
while(m.find())while(m2.find())
在这里发布之前,请确保您了解编程的基本概念和语言的语法。使用重复行可以做什么?请帮助我!如何改进此程序?拆分您的
while()while()
循环成两个不同的循环。我不知道如何使用它。也许,你能给我发一份完整的代码吗?@phatfingers,我想你已经尽了你所能把这匹马引向了水。顺便说一句,指向样本输入和所需输出的链接似乎不再起作用了。