Regex / Java - 如何捕获动态表中String之后的五位数

时间:2017-04-17 13:43:07

标签: java html regex selenium-webdriver

我在下面的表中,如果它与手机号码“00955555555555”相匹配,我正在阅读整行,并且需要获取代码“89721”。

table border="2" cellspacing="0" width="100%" cellpadding="0">

    手机号码     日期的     邮件

<td align="left" nowrap><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;00955555555555</font></td>
<td align="left" nowrap><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;2017-04-17 17:34:06.72</font></td>
<td align="left"><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;Your authentication code is  89721 to add name as beneficiary for payment from your account. If you have not requested to add this beneficiary, please contact the bank 

立即00971 600 54 0000。     

<td align="left" nowrap><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;955111111111</font></td>
<td align="left" nowrap><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;2017-04-17 17:31:13.893</font></td>
<td align="left"><font face="times new roman" size=3 >&nbsp;&nbsp;&nbsp;Your authentication code is: 91518. Please do not share this code with any person.</font></td>
<tr>

我尝试了以下代码,但它返回的是手机号码,但不是五位数代码。

代码:

public String entire_row_is_read_which_matches_with_the_Mobile_number() throws Throwable {

String mobilenumber="00955555555555"; 


//Date validate = null;
        {
            List<WebElement> rows = driver1.findElements(By.cssSelector("tr"));
            for (WebElement row : rows)
            {
                String text = row.getText();
                if (text.contains(mobilenumber))
                {
                   String regex = " (\\d+)"; //Your authentication code is

                   System.out.println(regex);

                    Pattern pattern = Pattern.compile(regex);
                    Matcher matcher = pattern.matcher(text);

                    if (matcher.find())             
                         {

                        valueis = matcher.group(1); 
                        System.out.println(valueis);

                        break;

                         }

2 个答案:

答案 0 :(得分:2)

我喜欢为这样的事情编写函数,因为它们很可能被重用。下面的函数接收您要搜索的手机号码并返回身份验证码。

public String GetAuthCode(String number)
{
    String code = driver
            .findElement(
                    By.xpath("//tr/td[contains(.,'" + number + "')]/following-sibling::td[contains(.,'Your authentication code')]"))
            .getText();
    String regex = "Your authentication code is: (\\d+)";
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(code);

    if (matcher.find())
    {
        return matcher.group(1);
    }

    return "";
}

答案 1 :(得分:-1)

您可以使用jsoup.jar获取所需的数据。 https://jsoup.org/

演示:

    String html = " <table border=\"2\" cellspacing=\"0\" width=\"100%\" cellpadding=\"0\">" + "<tr>"
            + "<td align=\"left\" nowrap><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;00955555555555</font></td>"
            + "<td align=\"left\" nowrap><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;2017-04-17 17:34:06.72</font></td>"
            + "<td align=\"left\"><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;Your authentication code is  89721 to add name as beneficiary for payment from your account. If you have not requested to add this beneficiary, please contact the bank</td>"
            + "</tr>" + "<tr>"
            + "<td align=\"left\" nowrap><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;955111111111</font></td>"
            + "<td align=\"left\" nowrap><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;2017-04-17 17:31:13.893</font></td>"
            + "<td align=\"left\"><font face=\"times new roman\" size=3 >&nbsp;&nbsp;&nbsp;Your authentication code is: 91518. Please do not share this code with any person.</font></td>"
            + "</tr>" + "</table>";

    Document doc = Jsoup.parse(html);
    String text = doc.select("tr >td:nth-child(2n+1)").text();
    Matcher m = Pattern.compile("\\d+").matcher(text);
    List<String> result = new ArrayList<String>();
    while (m.find()) {
        result.add(m.group());
    }
    System.out.println(result);

输出:

[00955555555555, 89721, 955111111111, 91518]