Regex/Java-如何在动态表中捕获字符串后的五位数字

Regex/Java-如何在动态表中捕获字符串后的五位数字,java,html,regex,selenium-webdriver,Java,Html,Regex,Selenium Webdriver,我在下表中读取整行,如果它与手机号码“00955555”匹配,并且需要获取代码“89721” 手机号码 日期 消息 009555 2017-04-17 17:34:06.72 您的身份验证代码是89721,用于添加姓名作为从您的帐户付款的受益人。如果您没有要求添加此受益人,请联系银行 立即于00971600 54 0000。 95511111111 2017-04-17 17:31:13.893 您的身份验证码是:91518。请不要与任何人共享此代码。 我试过下面的代码,但它返回的

我在下表中读取整行,如果它与手机号码“00955555”匹配,并且需要获取代码“89721”

手机号码 日期 消息

009555
2017-04-17 17:34:06.72
您的身份验证代码是89721,用于添加姓名作为从您的帐户付款的受益人。如果您没有要求添加此受益人,请联系银行
立即于00971600 54 0000。

95511111111
2017-04-17 17:31:13.893
您的身份验证码是:91518。请不要与任何人共享此代码。
我试过下面的代码,但它返回的是手机号码,而不是五位数的代码

代码:

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;

                         }
public String整个\u行\u是\u read\u,它与\u Mobile\u number()抛出的Throwable匹配{
字符串mobilenumber=“009555”;
//日期验证=空;
{
列表行=driver1.findElements(By.cssSelector(“tr”);
for(WebElement行:行)
{
String text=row.getText();
if(text.contains(mobilenumber))
{
String regex=“(\\d+”;//您的身份验证代码为
System.out.println(regex);
Pattern=Pattern.compile(regex);
Matcher Matcher=pattern.Matcher(文本);
if(matcher.find())
{
valueis=匹配器组(1);
系统输出打印项次(valueis);
打破
}

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

演示:


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

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 "";
}

不要使用正则表达式来解析html。那么?我如何才能将其用作参考。@vallentin他不是。他正在使用Selenium并试图解析没有html标记的结果文本。这些数据是什么?这不是第一次问这个问题。您的代码是我的答案,只是做了一些小改动。谢谢,它起了作用。我问的是同一个问题,但是re是我之前工作的表中的差异。
<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;

                         }
    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]
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 "";
}