java htmlunit 中的正则表达式

Regex expression in java htmlunit

我正在尝试通过自动化网页抓取和表单输入来提高我对 java 的了解。我已经尝试过 jsoup,现在是 htmlunit。我找到了一个我正在尝试 运行.

的 htmlunit 示例
public class GoogleHtmlUnitTest {
    static final WebClient browser;

    static {
        browser = new WebClient();
        browser.getOptions().setJavaScriptEnabled(false);
//        browser.setJavaScriptEnabled(false);
    }

    public static void main(String[] arguments) {
        boolean result;
        try {
            result = searchTest();
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }

        System.out.println("Test " + (result? "passed." : "failed."));
        if (!result) {
            System.exit(1);
        }
    }

    private static boolean searchTest() {
        HtmlPage currentPage;

        try {
            currentPage = (HtmlPage) browser.getPage("http://www.google.com");
        } catch (Exception e) {
            System.out.println("Could not open browser window");
            e.printStackTrace();
            return false;
        }
        System.out.println("Simulated browser opened.");

        try {
            ((HtmlTextInput) currentPage.getElementByName("q")).setValueAttribute("qa automation");
            currentPage = currentPage.getElementByName("btnG").click();
            System.out.println("contents: " + currentPage.asText());
            return containsPattern(currentPage.asText(), "About .* results");
        } catch (Exception e) {
            System.out.println("Could not search");
            e.printStackTrace();
            return false;
        }
    }

    public static boolean containsPattern(String string, String regex) {
        Pattern pattern = Pattern.compile(regex);

        // Check for the existence of the pattern
        Matcher matcher = pattern.matcher(string);
        return matcher.find();
    }
}

它适用于一些 htmlunit 错误,我在 Whosebug 上发现这些错误可以忽略。程序 运行s 正确,所以我接受了建议并忽略了错误。

Jul 31, 2016 7:29:03 AM com.gargoylesoftware.htmlunit.DefaultCssErrorHandler error
WARNING: CSS error: 'https://www.google.com/search?q=qa+automation&sa=G&gbv=1&sei=_eCdV63VGMjSmwHa85kg' [1:1467] Error in declaration. '*' is not allowed as first char of a property.

我目前的问题是用于搜索的正则表达式。如果我的理解正确,“qa automation”正在被谷歌搜索,检索到的页面正在被搜索:

return containsPattern(currentPage.asText(), "About .* results");

让我感到困惑的是“关于 .* 结果”。这是正则表达式,但我不明白它是如何被解释的。在检索到的页面上搜索什么?

.* 表示 "zero or more of any character," 换句话说,一个完整的通配符。可以是

About 28 results
About 2864 results
About 2,864 results
About ERROR results
About  results

(回复评论。)

老实说,您应该找一个快速的正则表达式教程。您遗漏了一些非常基本的东西,而是依赖于您自己对 "searching" 应该如何工作的直觉,这会导致混淆。

虽然我喜欢教学,所以这里还有一点:-)

前往 this RegExr link。我已经用这个表达式设置了它:

/^About .* results$/gm

忽略 /^$/gm。 (如果你真的想知道,两个斜杠只是正则表达式的常规符号。^$ 是 "anchors" 强制 "full match"——这就是为什么它似乎 "About" 必须位于位置 0。无论您使用的是什么正则表达式引擎,它似乎都会强制锚定。g 是一个标志,仅表示 "Highlight every match," 而 m 是一个标志,表示 "Treat every line as a separate entry.") 不管怎样,回到主要表达式:

About .* results

及其匹配项:

看看如果你把一个字符放在两边,它就不再匹配了?同样,这是因为锚定。该表达式期望 "A" 作为第一个字符,因此 "x" 失败。该表达式还期望最后一个字符是 "s",因此 "x" 也会在那里失败。但是为什么 About results 失败了呢?这是因为 .* 的每一边都有一个 space。 .* 通配符不能匹配任何内容, 但 space 必须像字母和数字一样匹配 。所以单个 space 不会削减它;你至少需要两个。

您写道您尝试过 230 .* results。看,您不了解正则表达式是逐个字符工作的,您可以使用某些 "special" 个字符。你的表达意思是,"A string that begins with 230, a space, then anything, a space, "results",之后什么都没有。"

[...] how would I code regex to find the "230" in any position followed by "results", ie "foobar 230 foobar2 results"?

换句话说,你想找到一个以任何东西开头的字符串,某处有 230,有更多的东西,space,"results",仅此而已:

.*230.* results

你想要确切的数字吗,230?

.* 230 results