Java 怎样把图案弄清楚一点?

Java 怎样把图案弄清楚一点?,java,regex,Java,Regex,我试图用regexp解析html页面,但我想要编译的模式看起来像怪物 public static void main(String[] args) { String form = "123123" + "<input type=\"hidden\" name=\"ip_h\" value=\"8d25cea553b4afe087\" />\n" + "<input type=\"hidden\"

我试图用regexp解析html页面,但我想要编译的模式看起来像怪物

public static void main(String[] args)  {
        String form = "123123" +
                "<input type=\"hidden\" name=\"ip_h\" value=\"8d25cea553b4afe087\" />\n" +
                "<input type=\"hidden\" name=\"lg_h\" value=\"e04c5b67874fd6e28b\" />\n" +
                "<input type=\"hidden\" name=\"_origin\" value=\"https://oauth.site.com\" />\n"+
                 "<input type=\"hidden\" name=\"to\" value=\"aHR0cHM6Ly9vYXV0aC52ay5jb20vYXl\" />";
        Pattern pattern = Pattern.compile(".*\"ip_h\".value=\"([a-z0-9]*)\".*\\s.*lg_h\".value=\"([a-z0-9]*)\".*\\s.*\\s.*to\".value=\"([a-zA-Z0-9]*)\".*");
        Matcher matcher = pattern.matcher(form);
        matcher.matches();
        System.out.println(matcher.group(1)+matcher.group(2)+matcher.group(3));

    }
publicstaticvoidmain(字符串[]args){
字符串form=“123123”+
“\n”+
“\n”+
“\n”+
"";
Pattern Pattern=Pattern.compile(“%a-z0-9]*)\”([a-z0-9]*)\“*\\s.*lg\u h\”。value=\”([a-z0-9]*)\“*”。*\\s.\\s.*to\”。value=\”([a-zA-z0-9]*)\”;
Matcher Matcher=pattern.Matcher(form);
matcher.matches();
系统输出println(matcher.group(1)+matcher.group(2)+matcher.group(3));
}
有没有机会说清楚一点? 为什么我有“\n”时总是使用\s?(*)-是否也匹配所有内容和空格?
另外,如果我想获得更多关于错误的信息,我该怎么办呢?不仅仅是:“线程中的异常”main“java.lang.IllegalStateException:找不到匹配项”

在我看来,为了完成手头的任务,我认为您只是过度考虑了正则表达式。保持表达式尽可能简单易读始终是一个好主意。很明显,你已经意识到了这一点

这里显然需要分组,因为您只需要与该数据特定相关的名称中的特定数据。由于您已经知道名称,因此需要从中获取特定数据,这使事情变得更简单:

现在我不知道网页内容由什么组成,但我们将讨论几个场景。你的处境很可能是两者中的后者

场景1:

如果网页内容中只有特定数量的行,包括:

<input type="hidden" name="ip_h" value="8d25cea553b4afe087" />
<input type="hidden" name="lg_h" value="e04c5b67874fd6e28b" />
<input type="hidden" name="_origin" value="https://oauth.site.com" />
<input type="hidden" name="to" value="aHR0cHM6Ly9vYXV0aC52ay5jb20vYXl" />
也就是说:

遍历整个字符串并将中的所有内容分组到类似
value=“
的子字符串和双引号字符()之间,因为所需的值位于双引号内

现在只需迭代找到的项来收集数据:

while (matcher.find()) {
    System.out.println(matcher.group(1));
}
现在,如果网页内容中还有其他行也包含一个名为
value=
的键,并且其值也在双引号内,那么这一点就不会很好地工作。上面的正则表达式示例也会提取数据,这显然是不好的。正则表达式只是不够具体,这就引出了下一个场景

场景2:

在这个特定场景中,网页内容包含许多行,这些行有一个名为
value=
的键,其相关数据值在双引号()内。显然,我们不希望它们都是正则表达式,因此我们需要更具体地使用正则表达式。既然我们知道内容中与name键相关的名称,那么我们试试看:

"(\"ip_h\"|\"lg_h\"|\"to\").*value\\=\"(.*?)\""
这里我们使用两个组,我们需要的实际数据将包含在第二个组中。第一个组使用正则表达式或运算符(|)来保持简单。上面的正则表达式基本上是这样说的:

沿着整个字符串工作,如果遇到一个名为“ip_h”或名为“ip_h”或名为“to”,并且在该名称后面的某个地方有一个子字符串,看起来像value=“”,然后获取介于value之间的数据=“,下一个遇到双引号”
,将其放入第2组

现在只需迭代找到的项,以收集组2中包含的数据:

while (matcher.find()) {
    System.out.println(matcher.group(2));
}
如果您发现仍然需要更具体地容纳正则表达式才能这样做。您可以添加另一个组。如果您发现它变得越来越大,则可以将表达式放入字符串变量(如果愿意)

String regEx = "(\\<input type\\=\"hidden\".*)(\"ip_h\"|\"lg_h\"|\"to\").*value\\=\"(.*?)\"";

Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(form);

// The required data ca be obtained from Group 3

String regEx=“(\\n不要使用regEx来解析html。你可以使用它。@JEY但是仅仅为html页面中的3个值使用外部库是过分的,不是吗?当然,但是如果你想要一个更可读的代码来处理每种可能的情况,这是最好的方法(实现与jsoup等价的东西需要很长时间)。如果您确定html是有效的xml,则可以使用DOM或SAX对其进行解析。
String regEx = "(\\<input type\\=\"hidden\".*)(\"ip_h\"|\"lg_h\"|\"to\").*value\\=\"(.*?)\"";

Pattern pattern = Pattern.compile(regEx);
Matcher matcher = pattern.matcher(form);

// The required data ca be obtained from Group 3
String form = "123123" +
        "<input type=\"hidden\" name=\"ip_h\" value=\"8d25cea553b4afe087\" />\n" +
        "<input type=\"hidden\" name=\"lg_h\" value=\"e04c5b67874fd6e28b\" />\n" +
        "<input type=\"hidden\" name=\"_origin\" value=\"https://oauth.site.com\" />\n"+
        "<input type=\"hidden\" name=\"to\" value=\"aHR0cHM6Ly9vYXV0aC52ay5jb20vYXl\" />\n";

Pattern pattern = Pattern.compile("(\"ip_h\"|\"lg_h\"|\"to\").*value\\=\"(.*?)\"");
Matcher matcher = pattern.matcher(form);
List<String> foundValues = new ArrayList<>();
while (matcher.find()) {
    foundValues.add(matcher.group(2));
}

// Display List in Console...
for (int i = 0; i < foundValues.size(); i++) {
    System.out.println(foundValues.get(i));
}

// If you want to have all the found items placed
// into a one Dimensional String Array then you can
// use this code:
String[] itemsFound = foundValues.toArray(new String[0]);

// Display Array in Console...
System.out.println();
for (int i = 0; i < itemsFound.length; i++) {
    System.out.println(itemsFound[i]);
}