如何从全文中选择特定片段?

问题描述 投票:0回答:1

在网站上注册后,我通过邮件收到了格式为的凭证。

Some text login: [email protected] password: example123 some text.

我需要准确地选择和复制登录名和密码,不要有太多的文字。所有的文字都位于一个表格中。不知道如何做到这一点。我将非常感谢如何做到这一点的想法。

java selenium automation
1个回答
0
投票

你可以将字符串拆分(快而脏)。

        String input = "some text / login: [email protected] / password: example123 / some text";
       // iterate over lines if necessary or join using a stream.join("\n")
       String username = input.split("login: ").split(" ")[0];
       String password = input.split("password: ").split(" ")[0];

可能还有很多其他的方法,其他人也可以建议。

或者使用regex和模式匹配。

        String input = "some text / login: [email protected] / password: example123 / some text";
        String emailRegex = "login: .*@.*\\..* ";
        Pattern pattern = Pattern.compile(emailRegex, Pattern.CASE_INSENSITIVE);

        Matcher matcher = pattern.matcher(input);

        while (matcher.find()) {
            String loginEmail = matcher.group();
            System.out.println(matchingText); // would need to split it
        }

和密码一样

如果你想要的东西是可扩展的,更容易管理,使用Regex。如果你不关心性能,我认为第一种选择是比较直接的,但如果电子邮件格式发生重大变化,你可能需要维护它。

© www.soinside.com 2019 - 2024. All rights reserved.