Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的正则表达式不是';我的java程序中没有匹配的东西_Java_Regex - Fatal编程技术网

我的正则表达式不是';我的java程序中没有匹配的东西

我的正则表达式不是';我的java程序中没有匹配的东西,java,regex,Java,Regex,我试图匹配以下两个示例之一: 示例输入1: <a href="Substance/acide_flavodique-4454.htm">acide flavodique</a> 示例输入2: <a href="Medicament/ciprofloxacine_arrow_750_mg_cp_pellic-71371.htm">CIPROFLOXACINE ARROW 750 mg cp pellic</a> 我需要在我的文件中打印的是

我试图匹配以下两个示例之一:

示例输入1:

<a href="Substance/acide_flavodique-4454.htm">acide flavodique</a>

示例输入2:

<a href="Medicament/ciprofloxacine_arrow_750_mg_cp_pellic-71371.htm">CIPROFLOXACINE ARROW 750 mg cp pellic</a>

我需要在我的文件中打印的是:1-acide flavodique:如果它与第一个示例匹配。 2-环丙沙星:如果它与第二个示例相匹配。 我的正则表达式或其他东西有什么问题吗? 提前谢谢

BufferedReader lire = new BufferedReader(new FileReader(file1));
            do{         
                String line = lire.readLine();



                if(line == null)
                {
                    break;
                }
                Pattern p = Pattern.compile ("<a href=\"Substance/.+>(.+)</a>|<a href=\"Medicament/.+>(.+)\\s+.+</a>");
                Matcher m = p.matcher(line); System.out.println("match:"+m.group(1)+"\n");
                if (m.matches()) {
                writer.write(line);
                writer.write(System.getProperty("line.separator"));
                }
            }while(true);


            //      }
            writer.close();
        }}}
BufferedReader lire=new BufferedReader(new FileReader(file1));
做{
String line=lire.readLine();
如果(行==null)
{
打破
}
模式p=Pattern.compile(“|”);
Matcher m=p.Matcher(行);System.out.println(“匹配:“+m.group(1)+”\n”);
如果(m.matches()){
作者:写(行);
writer.write(System.getProperty(“line.separator”);
}
}虽然(正确);
//      }
writer.close();
}}}
您的模式是:

<a href=\"Substance/.+>(.+)</a>|<a href=\"Medicament/.+>(.+)\\s+.+</a>
|
这包含一些“/”字符,这些字符被认为是不可替换的,从而使您的模式无效。您可以在此处测试这些内容:

您调用m.group(..)太早了。您应该首先调用m.matches(),否则会得到非法状态异常

顺便说一下,找到了模式(至少您提供的两个示例是匹配的)。

第一个问题:

这不起作用,因为在调用
m.group(1)
之前必须先调用
m.matches()
。 所以这会更好:

   Matcher m = p.matcher(line); 
   if (m.matches()) {
       System.out.println("match:"+m.group(1)+"\n");
       // ...
   }
第二个问题是群体问题。 鉴于这种模式:

Pattern p=Pattern.compile(“|”);
这些输入:

String line1 = "<a href=\"Substance/acide_flavodique-4454.htm\">acide flavodique</a>";
String line2 = "<a href=\"Medicament/ciprofloxacine_arrow_750_mg_cp_pellic-71371.htm\">CIPROFLOXACINE ARROW 750 mg cp pellic</a>";
字符串行1=“”;
字符串line2=“”;
这两条线都将匹配,但匹配的零件将位于不同的组中。 对于
第1行
,“acide flavodique”将在
组(1)
中, 但对于
line2
,“环丙沙星箭头750 mg cp”将在
组(2)
中。
这是因为在正则表达式中有两个
(…)

除非您关心找到哪一个,否则我想您可以将这两个组合起来,这样就可以了
需要的是单个捕获组1

 #  "<a\\s+href\\s*=\\s*\"\\s*(?:Substance|Medicament)/[^>]+>([\\s\\S]+?)</a>"

 <a \s+ href \s* = \s* " \s* 
 (?: Substance | Medicament )
 / [^>]+ 
 >
 ( [\s\S]+? )                  # (1)
 </a>
#“”
看一看
String line1 = "<a href=\"Substance/acide_flavodique-4454.htm\">acide flavodique</a>";
String line2 = "<a href=\"Medicament/ciprofloxacine_arrow_750_mg_cp_pellic-71371.htm\">CIPROFLOXACINE ARROW 750 mg cp pellic</a>";
 #  "<a\\s+href\\s*=\\s*\"\\s*(?:Substance|Medicament)/[^>]+>([\\s\\S]+?)</a>"

 <a \s+ href \s* = \s* " \s* 
 (?: Substance | Medicament )
 / [^>]+ 
 >
 ( [\s\S]+? )                  # (1)
 </a>