regex 我已经使用java提取了一封电子邮件,现在我想从中提取OTP,如何/ [closed]

9fkzdhlc  于 2022-11-18  发布在  Java
关注(0)|答案(1)|浏览(161)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。

17小时前就关门了。
Improve this question
我已经写了一个自动化脚本发送OTP电子邮件。我已经存储在一个字符串列表的电子邮件。下面是代码:

public static void main(String[] args) throws FileNotFoundException, Exception{
        
        EmailUtils emailUtils = new EmailUtils();
        
        Properties prop = new Properties();
        prop.load(new FileInputStream("/Users//Automation/Automation/src/main/java/Resources/config-email-otp.properties"));
        Store connection = emailUtils.connectToGmail(prop);
        emailUtils.getUnreadMessages(connection, "Inbox");
        
        List<String> emailtext = emailUtils.getUnreadMessageByFromEmail(connection, "Inbox", "no-reply@xxxx.in", "Email ID verification");

现在,我想从电子邮件中提取OTP。
通过电子邮件发送它在控制台中的显示方式:

This is the message envelope
---------------------------
FROM: dummyemail@gmail.com
TO: dummyemail@gmail.com
SUBJECT: Email ID verification 
----------------------------
CONTENT-TYPE: TEXT/HTML; charset=us-ascii
This is a string
---------------------------
<!doctype html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.t.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Email verification</title>
</head>
<body>
<div>Dear User,</div>

<div>You are performing account verification operation , the verification code is:  <b>4740</b></div>

<div>This verification code can only be used once, and it will automatically become invalid after verification.</div>

<div>(Please complete the verification within 30 minutes, the verification code will become invalid after 30 minutes, you need to verify again.)</div>

<div>Please ignore this email if we send the wrong address, thanks.</div>

<div>team</div>

</body>
</html>
mspsb9vt

mspsb9vt1#

String emailText = "Your email text"; 
        String pattern = "code is:  <b>(\d{4}+)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(line);

        if (m.find()) {
            System.out.println("Found value: " + m.group(0));
            System.out.println("Found value: " + m.group(1));
            System.out.println("Found value: " + m.group(2));
        } else {
            System.out.println("NO MATCH");
        }
    }

您需要群组1的值。如果只使用\d{4},您也会符合w3.org的年份。x1c 0d1x
完整示例:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexMatch {
    public static final String EMAIL = "This is the message envelope\n" +
            "---------------------------\n" +
            "FROM: dummyemail@gmail.com\n" +
            "TO: dummyemail@gmail.com\n" +
            "SUBJECT: Email ID verification \n" +
            "----------------------------\n" +
            "CONTENT-TYPE: TEXT/HTML; charset=us-ascii\n" +
            "This is a string\n" +
            "---------------------------\n" +
            "<!doctype html>\n" +
            "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"\n" +
            "      xmlns:th=\"http://www.t.org\">\n" +
            "<head>\n" +
            "    <meta charset=\"UTF-8\">\n" +
            "    <meta name=\"viewport\"\n" +
            "          content=\"width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0\">\n" +
            "    <meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">\n" +
            "    <title>Email verification</title>\n" +
            "</head>\n" +
            "<body>\n" +
            "<div>Dear User,</div>\n" +
            "\n" +
            "\n" +
            "<div>You are performing account verification operation , the verification code is:  <b>4740</b></div>\n" +
            "\n" +
            "<div>This verification code can only be used once, and it will automatically become invalid after verification.</div>\n" +
            "\n" +
            "<div>(Please complete the verification within 30 minutes, the verification code will become invalid after 30 minutes, you need to verify again.)</div>\n" +
            "\n" +
            "<div>Please ignore this email if we send the wrong address, thanks.</div>\n" +
            "\n" +
            "<div>team</div>\n" +
            "\n" +
            "</body>\n" +
            "</html>\n" +
            "javaregexseleniumui-automation";

    public static void main(String[] args) {
        String pattern = "code is:  <b>(\\d{4}+)";

        // Create a Pattern object
        Pattern r = Pattern.compile(pattern);

        // Now create matcher object.
        Matcher m = r.matcher(EMAIL);

        if (m.find()) {
            System.out.println("Found value: " + m.group(0));
            System.out.println("Found value: " + m.group(1));
        } else {
            System.out.println("NO MATCH");
        }

    }
}

相关问题