如何在java中的字符串中使用正则表达式? [复制]

2024-03-22

嗨,我有一个字符串,我想使用正则表达式或任何方法来打破它

我的字符串是

  1 Agra Achhnera NIL
  2 Agra Agra NIL
  3 Agra Fatehabad NIL
  4 Agra Fatehpur Sikri NIL
  5 Aligarh Aligarh 1300.00
  6 Aligarh Khair 1300.00
  7 Ambedkar Nagar Akbarpur NIL
  8 Ambedkar Nagar Tanda Akbarpur 1478.00

结果我想要这样的字符串:-

1 Agra Achhnera NIL
2 Agra Agra NIL
3 Agra Fatehabad NIL
4 Agra FatehpurSikri NIL
5 Aligarh Aligarh 1300.00
6 Aligarh Khair 1300.00
7 AmbedkarNagar Akbarpur NIL
18 AmbedkarNagar TandaAkbarpur 1478.00

我怎样才能做到这一点

我的java代码

<%@page import="com.gargoylesoftware.htmlunit.BrowserVersion;"%>
<%@page import="org.openqa.selenium.By;"%>
<%@page import="org.openqa.selenium.WebDriver;"%>
<%@page import="org.openqa.selenium.WebElement"%>
<%@page import="org.openqa.selenium.firefox.FirefoxDriver"%>
<%@page import="org.openqa.selenium.htmlunit.HtmlUnitDriver"%>
<%@page import="org.openqa.selenium.support.ui.Select"%>

<%


 WebDriver driver = new HtmlUnitDriver(BrowserVersion.getDefault());
        String sDate = "27/03/2014";

        String url="http://www.upmandiparishad.in/commodityWiseAll.aspx";
        driver.get(url);
        Thread.sleep(5000);

        new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Jo");
         driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);

        Thread.sleep(3000);
        driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
        Thread.sleep(5000);


        WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
        String htmlTableText = findElement.getText();
        // do whatever you want now, This is raw table values.
   htmlTableText=htmlTableText.replace("S.No.DistrictMarketPrice","");
  htmlTableText= htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "<br>$1");
   //System.out.println(htmlTableText);
String data[]=htmlTableText.split("");
   out.println(data[9]);



        driver.close();
        driver.quit();
%>

提前致谢


这将适用于您的特定情况,因为您正在抓取的数据采用这种格式,并且以下正则表达式在大多数情况下都适用。我并不是说这是完美的答案,但我应该告诉你,这会让你继续前进:

find

[a-z]\s[A-Z]

并替换为\1\2

演示在这里:http://regex101.com/r/kF8vG6 http://regex101.com/r/kF8vG6

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在java中的字符串中使用正则表达式? [复制] 的相关文章

  • 如何自定义BlockingQueue的阻塞行为

    我想创建一个阻塞队列 它根据自定义规则而不是队列中的项目数量来阻止生产者 例如 生产者生成一些文件并放入队列中 消费者经过一番分析后将它们转移到特定位置 对于上述场景 如果队列中的总文件大小达到某个阈值 我希望生产者等待生成新文件 如果总大
  • JavaEE 8 教程,在 hello1 项目上部署失败

    我正在尝试学习 Java EE 8 我遵循了官方指南https javaee github io tutorial https javaee github io tutorial 但我有这个问题 cargo maven2 plugin 1
  • 在 Kotlin 中实现返回 Collection 的 Java 方法

    我将 Kotlin 与 Spring Security 结合使用 实现该方法时 public interface UserDetails extends Serializable Collection
  • Java Swing:清除JList而不触发监听器

    我的情况如下 我有一个 JList 只要在列表中进行选择 它就会触发搜索 使用 ListSelectionListener 我正在尝试使用以下命令重置列表上的选择list clearSelection 这样做的问题是使用clearSelec
  • 以编程方式将 PEM 证书导入 Java KeyStore

    我有一个由两个文件 crt 和 key 组成的客户端证书 我希望将其导入到 java KeyStore 中 然后在 SSLContext 中使用 以通过 Apache 的 HTTPClient 发送 HTTP 请求 但是 我似乎找不到一种以
  • 从 eclipse 运行时 java.io.FileNotFoundException: (没有这样的文件或目录)

    我正在写入文件并想要控制台输出 TODO Create a game engine and call the runGame method public static void main String args throws Excepti
  • 业务代表与服务定位器

    Business Delegate 和 Service Locator 之间有什么区别 两者都负责封装查找和创建机制 如果 Business Delegate 使用 Service Locator 来隐藏查找和创建机制 那么 Busines
  • 如何使用 Java 原生接口从 Java 调用 Go 函数?

    可以通过以下方式调用 C 方法JNA https en wikipedia org wiki Java Native AccessJava 中的接口 如何使用 Go 实现相同的功能 package main import fmt impor
  • PHP使用正则表达式查找字符串

    我已经阅读了多个有关正则表达式的教程 但它只是不会留在我的脑海中 我永远无法让我的模式发挥作用 希望有人能帮忙 我有一个 php 变量 content 我需要在其中找到如下所示的特定模式 图库 名称 文件夹 我想搜索 starting wi
  • Java String.format 向整数添加空格

    我有一小段代码 我不明白输出 此输出向我的字符串格式文本添加空格 我做错了什么吗 public class HelloWorld public static void main String args int a1 540 int a2 4
  • java 属性文件作为枚举

    是否可以将属性文件转换为枚举 我有一个包含很多设置的属性文件 例如 equipment height equipment widht equipment depth and many more like this and not all a
  • Java中的DRY原则[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 我一直在读关于DRY https en wikipedia org wiki Don 27t repeat yourself原则 虽然看起来
  • 用 Java 创建迷宫求解算法

    我被分配了用 Java 创建迷宫求解器的任务 这是任务 Write an application that finds a path through a maze The maze should be read from a file A
  • RMI 服务器:rmiregistry 或 LocateRegistry.createRegistry

    对于服务器端的RMI 我们需要启动吗rmiregistry程序 或者只是调用LocateRegistry createRegistry 如果两者都可以的话 各有什么优点和缺点 他们是同一件事 rmiregistry是一个单独的程序 您可以从
  • 将字符串中的字符向左移动

    我是 Stack Overflow 的新手 有一道编程课的实验室问题一直困扰着我 该问题要求我们将字符串 s 的元素向左移动 k 次 例如 如果输入是 Hello World 和3 它将输出 lo WorldHel 对于非常大的 k 值 它
  • Scala repl 抛出错误

    当我打字时scala在终端上启动 repl 它会抛出此错误 scala gt init error error while loading AnnotatedElement class file usr lib jvm java 8 ora
  • Android 中的字符串加密

    我正在使用代码进行加密和加密 它没有给出字符串结果 字节数组未转换为字符串 我几乎尝试了所有方法将字节数组转换为字符 但没有给出结果 public class EncryptionTest extends Activity EditText
  • 防止 Firebase 中的待处理写入事务不起作用

    我的目标是在单击按钮时将名称插入 Cloud Firestore 中 但如果用户未连接到互联网 我不希望保存处于挂起状态 我不喜欢 Firebase 保存待处理写入的行为 即使互联网连接已恢复 我研究发现Firebase 开发人员建议使用事
  • Android同步onSensorChanged?

    这是我的问题的后续 Android线程可运行性能 https stackoverflow com questions 36395440 android thread runnable performance 我在理解应用程序的同步方法时遇到
  • Java 9 中紧凑字符串和压缩字符串的区别

    有什么优点紧凑的字符串 http openjdk java net jeps 254JDK9 中的压缩字符串 压缩字符串 Java 6 和紧凑字符串 Java 9 都有相同的动机 字符串通常实际上是 Latin 1 因此浪费了一半的空间 和

随机推荐