【问题标题】:Replacing all matched strings in a file with matcher.replaceall in Java用Java中的matcher.replaceall替换文件中的所有匹配字符串
【发布时间】:2013-11-21 09:23:50
【问题描述】:

我正在尝试使用matcher.replaceAll()替换文件中的仅匹配的字符串。如下所示

public static void main(String[] args) throws IOException, BadLocationException {

      FileInputStream fis = null;
      String output = new Scanner(new File("C:/file.in")).useDelimiter("\\Z").next();//file.in contains HTML text
      String s2 = null;
      StringWriter writer = new StringWriter();
      for (int i=0; i<patternList.size(); i++)//PatternList contains names to be extracted from file
      {
          Matcher matcher = patternList.get(i).matcher(output);
          while (matcher.find()) 
          {
                String matched = matcher.group();//I need to replace only matched strings returned by matcher
                s2=matcher.replaceAll("<span style='background-color:red;'>"+matched+"</span>");
      File file = new File("C:/data/filename.in");
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(s2);
        bw.close();
          }   
      }

结果字符串s2 仅更新了 PatternList 中的最后一个字符串。每次我用新匹配的字符串覆盖字符串。如何获得使用所有匹配字符串(模式列表中的名称)更新的最终大字符串。

【问题讨论】:

    标签: java regex string matcher


    【解决方案1】:

    我相信你有很多冗余代码。您绝对不需要Pattern 对象的列表。

    考虑这段代码:

    String output = new Scanner(new File("C:/file.in")).useDelimiter("\\Z").next();
    String repl = output.replaceAll("\b(Shannon|Sperling|Kim|Tammy|Nancy|Lana)\b", 
                  <span style='background-color:red;'>$1</span>"); 
    
    File file = new File("C:/data/filename.in");
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write( repl );
    bw.close();
    

    【讨论】:

    • 为了简单起见,我只是在示例中显示了 6 个字符串,实际上我有数千个字符串要更新。这只能通过“匹配器”来实现。每当比赛发生时,我都需要更新它。
    • 这些字符串存储在字典中,有时它们可​​能是 1000 的倍数。我正在阅读字典中的条目,构建模式然后检查文件中的存在。你说的方式没有看起来不错。事实上,根本不可能得到所有字符串并用“|”把它放在那里作为分隔符。
    • 每个单词词典都可以返回您的字符串单词,所以不确定您在说什么。在您的问题中,您有String[] names。想想你的问题没有得到任何答案的原因。
    • 您的代码可以很好地替换指定文件中的字符串。因为您要替换一行中的所有字符串并分配给结果字符串。但我的要求不同。首先我必须获取字典条目,建立一个模式并使用匹配器来获取匹配的字符串与开始和结束索引(这对我的项目非常重要),然后只需突出显示文件中匹配的字符串。我应该使用字典的原因是它们会改变不断地。假设我的字典中有 3000 个名字,它是如何与你的代码一起工作的。
    【解决方案2】:

    s2 更改为output 后,我能够解决问题。将结果(matcher.replaceAll)分配给output 而不是s2。现在所有匹配的字符串都替换为颜色标签。

    public static void main(String[] args) throws IOException, BadLocationException {
    FileInputStream fis = null;
      String output = new Scanner(new File("C:/file.in")).useDelimiter("\\Z").next();//file.in contains HTML text
      String s2 = null;
      StringWriter writer = new StringWriter();
      for (int i=0; i<patternList.size(); i++)//PatternList contains names to be extracted from file
      {
          Matcher matcher = patternList.get(i).matcher(output);
          while (matcher.find()) 
          {
                String matched = matcher.group();//I need to replace only matched strings returned by matcher
                output=matcher.replaceAll("<span style='background-color:red;'>"+matched+"</span>");
      File file = new File("C:/data/filename.in");
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write(output);
        bw.close();
          }   
      }
    

    【讨论】:

      猜你喜欢
      • 2023-04-01
      • 2016-09-26
      • 2014-08-14
      • 2017-01-10
      • 1970-01-01
      • 2022-11-19
      • 2011-08-29
      • 2015-07-23
      • 2013-12-31
      相关资源
      最近更新 更多