java - 如何从大量文本内容中提取特定文本?

标签 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 中处理这个问题。

最佳答案

您可以使用吐出字符串

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);

您将轻松获得所需的 URL。

关于java - 如何从大量文本内容中提取特定文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019022/

相关文章:

java - 在 Firefox 上浏览网页上的链接时出现 WebDriver 异常 - Firefox Web 浏览器

java - org.hibernate.LazyInitializationException : failed to lazily initialize a collection of role: studentsystem2. ikubinfo.entity.Student.classes

java - Eclipse indigo Struts 2安装

java - 简单的 MPEG 视频流播放器,跨平台,在 java 中,通过 NetBeans + Swing?

java - 对象...对象[] 和格式

java - 如何使用 selenium 在表格内找到工具提示 <a href>tooltip</a> 值

java - 如何使用android以稳定的方式连接到没有互联网的ESP8266接入点?

java - 如何运行 headless Selenium 脚本,主机和浏览器独立

java - Selenium-Java : Wait. 直到(ExpectedConditions.invisibilityOfElementLocated()) 在负面情况下总是失败

c# - Selenium 2(网络驱动程序): Taking a Screenshot returns a black image