简体   繁体   English

Java替换文件正则表达式

[英]Java replacing in a file regex

I want to put in a file some regex expressions and separated by a semicolon (or something) another expression, ie: 我想在文件中放入一些正则表达式,并用分号(或其他)分隔另一个表达式,即:

orderNumber:* completionStatus;orderNumber:X completionStatus

I will have a log file what will have: 我将有一个日志文件将有:

.... orderNumber:123 completionStatus...

and I want them to look like: 我希望他们看起来像:

.... orderNumber:X completionStatus...

How can I do this in Java? 我怎么能用Java做到这一点?

I've tried creating a Map with (key: the regex, and value: the replacement), reading my log file and for each line try matching the keys but my output looks the same. 我已经尝试使用(键:正则表达式和值:替换)创建一个Map,读取我的日志文件,并尝试匹配键,但我的输出看起来相同。

FileInputStream fstream = new FileInputStream(file);
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader( in ));
FileWriter fstreamError = new FileWriter(myFile.replace(".", "Replaced."));
BufferedWriter output = new BufferedWriter(fstreamError);

while ((strFile = br.readLine()) != null) {
    for (String clave: expressions.keySet()) {
        Pattern p = Pattern.compile(clave);
        Matcher m = p.matcher(strFile); // get a matcher object
        strFile = m.replaceAll(expressions.get(clave));
        System.out.println(strFile);
    }
}

Any thoughts on this? 有什么想法吗?

is this what you are looking for? 这是你想要的?

import java.text.MessageFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexpTests {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    String text = "orderNumber:123 completionStatus";
    String regexp = "(.*):\\d+ (.*)";
    String msgFormat = "{0}:X {1}";

    Pattern p = Pattern.compile(regexp);
    Matcher m = p.matcher(text);

    MessageFormat mf = new MessageFormat(msgFormat);
    if (m.find()) {
        String[] captures = new String[m.groupCount()];
        for (int i = 0; i < m.groupCount(); i++) {
            captures[i] = m.group(i + 1);
        }
        System.out.println(mf.format(msgFormat, captures));
    }

}

}

It seems like you are on a good path. 看起来你走的路很好。 I would however suggest several things: 不过我会建议几件事:

  1. Do not compile the regex every time. 不要每次都编译正则表达式。 You should have them all precomplied and just produce new matchers from them in your loop. 你应该让它们全部预编译,并在循环中从它们产生新的匹配器。
  2. You aren't really using the map as a map, but as a collection of pairs. 您并没有真正将地图用作地图,而是作为对的集合。 You could easily make a small class RegexReplacement and then just have a List<RegexReplacement> that you iterate over in the loop. 您可以轻松地创建一个class RegexReplacement ,然后只需要在循环中迭代的List<RegexReplacement>

class RegexReplacement { 
  final Pattern regex;
  final String replacement;
  RegexReplacement(String regex, String replacement) {
    this.regex = Pattern.compile(regex);
    this.replacement = replacement;
  }
  String replace(String in) { return regex.matcher(in).replaceAll(replacement); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM