如何从大型文本内容中提取特定文本?

时间:2016-05-04 05:17:04

标签: java selenium-webdriver

我有一个段落

Dear testUser testUser, 
You have been registered in the Request Management System. 
Your username is the email address to which this notification is addressed. 
Please click the link below to set your password: http://www.example.com/QA/ChangePassword.aspx?userName=example@yopmail.com&tempPassword=b0cb6ca7-2e52-4b80-8252-f5dac499cea3
You will receive further information from the our employee that has registered you in the system.
Thank you

在上面的段落中,我想提取用于生成新密码的URL。

我使用正则表达式,我能够实现它。

以下是我使用的代码

String REGEX="Dear testuser,testuser,|You have been registered in the Request Management System.|Your username is the email address to which this notification is addressed.|Please click the link below to set your password:|You will receive further information from the our employee that has registered you in the system.|Thank you";
       if(copiedText.contains("password"))
                    {
                        copiedText=copiedText.replaceAll(REGEX, "").trim();
                        System.out.println("Final Text after replace: "+copiedText);
                        keyValues.put(key,copiedText);
                        System.out.println("the Values stored in map are:" + keyValues);

                    } 

我使用此获取URL但我认为这不是一个好方法。我需要一些通用解决方案,因为用户名也会在实时场景中发生变化。那么我们如何在java中处理这个问题。

1 个答案:

答案 0 :(得分:0)

您可以使用spit string

String expe= "Dear testUser testUser,"
            + "You have been registered in the Request Management System. "
            + "Your username is the email address to which this notification is addressed. "
            + "Please click the link below to set your password: http://www.example.com/QA/ChangePassword.aspx?userName=example@yopmail.com&tempPassword=b0cb6ca7-2e52-4b80-8252-f5dac499cea3"
            + "You will receive further information from the our employee that has registered you in the system."
            + "Thank you";


        String[] parts = expe.split("your password: ");
        String part2 = parts[1]; 
        parts = part2.split("You will receive further information");
        part2 = parts[0]; 
        System.out.println(part2);

您将轻松获得所需的网址。