簡體   English   中英

使用模式在文本之間替換Java正則表達式

[英]Java regex to replace in between text using a pattern

我是Java正則表達式的新手。 我有一個長字符串,其中包含這樣的文本(下面只是我要替換的字符串的一部分):

href="javascript:openWin('Images/DCRMBex_01B_ex01.jpg',480,640)"
href="javascript:openWin('Images/DCRMBex_01A_ex01.jpg',480,640)"
href="javascript:openWin('Images/DCRMBex_06A_ex06.jpg',480,640)"

我想替換

Images

http://google.com/Images

例如。 我的輸出應該如下所示:

href="javascript:openWin('http://google.com/Images/DCRMBex_01B_ex01.jpg',480,640)"

以下是我的Java程序:

import java.io.FileReader;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main2 {

    public static void main(String[] args) throws FileNotFoundException {

        Scanner in = new Scanner(new FileReader("C:\\Projects\\input.txt"));

        StringBuilder sb = new StringBuilder();
        while (in.hasNext()) {
            sb.append(in.next());
        }
        String patternString = "href=\"javascript:openWin(.+?)\"";
        Pattern pattern = Pattern.compile(patternString);
        Matcher matcher = pattern.matcher(sb);
        while (matcher.find()) {
            //System.out.println(matcher.group(1));
            //System.out.println(matcher.group(1).replaceAll("Images", "http://google.com/Images"));
            matcher.group(1).replaceAll("Images", "http://google.com/Images");

        }
        System.out.println(sb);
    }
}

下面是我的輸入文件(input.txt)。 這只是我文件的一部分。 該文件太長,無法在此處粘貼:

 <td valign="top"><a href="http://www.google.com/cds/desktop/documents/DCRMBex/DCRMBex_01_ex01.pdf"><b>Example 1: Bible (Rusch)</b></a> � <a href="javascript:openWin('Images/DCRMBex_01A_ex01.jpg',480,640)">Figure 1A. First page of text</a> � <a href="javascript:openWin('Images/DCRMBex_01B_ex01.jpg',480,640)">Figure 1B. Source of supplied title</a></td>
                            <td valign="top">  </td>
                            <td valign="top"><a href="http://www.google.com/cds/desktop/documents/DCRMBex/DCRMBex_06_ex06.pdf"><b>Example 6: Angelo Carletti</b></a> � <a href="javascript:openWin('Images/DCRMBex_06A_ex06.jpg',480,640)">Figure 6A. Title page</a> � <a href="javascript:openWin('Images/DCRMBex_06B_ex06.jpg',480,640)">Figure 6B. Colophon showing use of i/j and u/v</a></td>
                          </tr>
                          <tr>
                            <td valign="top"><a href="http://www.google.com/cds/desktop/documents/DCRMBex/DCRMBex_02_ex02.pdf"><b>Example 2: Greek anthology</b></a> � <a href="javascript:openWin('Images/DCRMBex_02A_ex02.jpg',480,640)">Figure 2A. First page of text</a> � <a href="javascript:openWin('Images/DCRMBex_02B_ex02.jpg',480,640)">Figure 2B. Colophon</a></td>
                            <td valign="top">  </td>
                            <td valign="top"><a href="http://www.google.com/cds/desktop/documents/DCRMBex/DCRMBex_07_ex07.pdf"><b>Example 7: Erasmus</b></a> � <a href="javascript:openWin('Images/DCRMBex_07A_ex07.jpg',480,640)">Figure 7A. Title page</a> � <a href="javascript:openWin('Images/DCRMBex_07B_ex07.jpg',480,640)">Figure 7B. Colophon</a> � <a href="javascript:openWin('Images/DCRMBex_07C_ex07.jpg',640,480)">Figure 7C. Running title</a></td>
                          </tr>
                          <tr>
                            <td valign="top"><a href="http://www.google.com/cds/desktop/documents/DCRMBex/DCRMBex_03_ex03.pdf"><b>Example 3: Heytesbury</b></a> � <a href="javascript:openWin('Images/DCRMBex_03A_ex03.jpg',480,640)">Figure 3A. Title page</a> � <a href="javascript:openWin('Images/DCRMBex_03B_ex03.jpg',480,640)">Figure 3B. Colophon showing use of i/j and u/v</a></td>
                            <td valign="top">  </td>
                            <td valign="top"><a href="http://www.google.com/cds/desktop/documents/DCRMBex/DCRMBex_08_ex08.pdf"><b>Example 8: Pliny</b></a> � <a href="javascript:openWin('Images/DCRMBex_08A_ex08.jpg',480,640)">Figure 8A. Title page</a> � <a href="javascript:openWin('Images/DCRMBex_08B_ex08.jpg',480,640)">Figure 8B. Colophon</a></td>

輸出:

1)System.out.println(matcher.group(1))

('Images/DCRMBex_05_ex05.jpg',480,640)

2)System.out.println(matcher.group(1).replaceAll(“Images”,“ http://google.com/Images ”));

 ('http://google.com/Images/DCRMBex_05_ex05.jpg',480,640)

但是,當我打印我的結構,它沒有顯示任何替代品。 我在這做錯了什么? 任何幫助表示贊賞。 謝謝

replaceAll返回修改后的字符串; 它沒有就地修改。 在這種情況下,我不會使用java.util.regex而是使用replaceAll捕獲組的支持:

Scanner in = new Scanner(new FileReader("C:\\Projects\\input.txt"));
StringBuilder sb = new StringBuilder();
while (in.hasNext()) {
    sb.append(in.next());
}
// Modified regex 
String patternString = "(href=\"javascript:openWin\\(')(.+?)(')";

String result = sb.toString().replaceAll(patternString, "$1http://google.com/$2$3");

在線嘗試

希望這可以幫助!

我建議使用Files.lines()和Java Steam來修改輸入。 根據您的實際輸入,您也不需要正則表達式:

try (Stream<String> lines = Files.lines(Paths.get("input.txt"))) {
    String result = lines
            .map(line -> line.replace("Images", "http://google.com/Images"))
            .collect(Collectors.joining("\n"));
    System.out.println(result);
}

如果你真的想使用正則表達式,我建議在循環外部使用一個模式,因為String.replaceAll()在每次調用它時都在內部編譯模式。 因此,如果您不為每行執行Pattern.compile() ,則性能會更好:

Pattern pattern = Pattern.compile("(href=\"javascript:openWin.*)(Images.*\")");
try (Stream<String> lines = Files.lines(Paths.get("input.txt"))) {
    String result = lines
            .map(pattern::matcher)
            .map(matcher -> matcher.replaceAll("$1http://google.com/$2"))
            .collect(Collectors.joining("\n"));
    System.out.println(result);
}

使用此正則表達式進行替換將創建兩個組(在()之間)。 您可以使用$index在替換字符串中使用此組。 所以$1將插入第一組。

兩種情況的結果都是:

href="javascript:openWin(&amp;#39;http://google.com/Images/DCRMBex_01B_ex01.jpg&amp;#39;,480,640)"
href="javascript:openWin(&amp;#39;http://google.com/Images/DCRMBex_01A_ex01.jpg&amp;#39;,480,640)"
href="javascript:openWin(&amp;#39;http://google.com/Images/DCRMBex_06A_ex06.jpg&amp;#39;,480,640)"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM