簡體   English   中英

遞歸地用正則表達式中的路徑替換正則表達式查找

[英]Recursively replace regex find with path in the regex

我的問題是一個設計問題。 我應該使用StringBuilder並逐行讀取文件,還是應該replace

我正在開發一種從文件中讀取文本的方法。 在文件中可以有@@GOTO:"C:\\path\\to\\more.txt"@@ 我想用兩個@@ s之間的所有內容替換C:\\path\\to\\more.txt 另外,在more.txt中可能還有另一個@@GOTO:"C:\\path\\to\\evenmore.txt"@@ 所以我認為遞歸這樣做是最好的。

我在考慮這樣的事情:

private String replaceGoTo(Pattern pattern, String xmlString) throws IOException {
  Matcher match = pattern.matcher(xmlString);
  while (match.find()) {
    String replacText = IOHelper.fileToString(match.group(2));
    String insert = replaceGoTo(pattern, replacText);
    xmlString = xmlString.replace(match.group(1), insert);
  }
  return xmlString;
}

但我想知道這是否是最有效的方法,因為我沒有使用StringBuilder而且我可能在這些字符串中有很多信息。

注意它沒有被打破,我只是在詢問是否有更好的方法來做到這一點。 謝謝!

最有效的方法是編寫一個FilterReader,在您讀取文件時替換起始字符串和結束字符串。 我認為不需要正則表達式來替換靜態數據。 以下代碼可以解決這個問題:

public class ReplaceFilterReader extends Reader {
    private String match;
    private String replace;

    private BufferedReader in;
    private boolean matched;
    private int currentPos;
    private boolean end = false;

    public ReplaceFilterReader(Reader in, String match, String replace) {
        this.in = new BufferedReader(in);
        this.matched = false;
        this.currentPos = 0;
        this.match = match;
        this.replace = replace;
    }

    @Override
    public int read(CharBuffer target) throws IOException {
        int len = target.remaining();
        char[] cbuf = new char[len];
        int n = read(cbuf, 0, len);
        if (n > 0) {
            target.put(cbuf, 0, n);
        }
        return n;
    }

    @Override
    public int read(char[] cbuf) throws IOException {
        return this.read(cbuf, 0, cbuf.length);
    }

    @Override
    public int read() throws IOException {
        char cb[] = new char[1];
        if (this.read(cb, 0, 1) == -1) {
            return -1;
        } else {
            return cb[0];
        }
    }

    private int readNext() throws IOException {
        int result = 0;
        if (!matched) {
            this.in.mark(match.length());
            char cb[] = new char[match.length()];
            int n = this.in.read(cb);
            if (n>0) {
                String s = new String(cb);
                if (s.equals(match)) {
                    this.matched = true;
                    if (replace.length()>0) {
                        result = replace.charAt(currentPos);
                        currentPos+=1;
                        if (currentPos == replace.length()) {
                            this.matched = false;
                            this.currentPos = 0;
                        }
                    } else {
                        this.matched = false;
                        this.currentPos = 0;
                        result = 0;
                    }

                } else {
                    this.in.reset();
                    result = this.in.read();
                }
            } else {
                result = n;
            }
        } else {
            result = replace.charAt(currentPos);
            currentPos+=1;
            if (currentPos == replace.length()) {
                this.matched = false;
                this.currentPos = 0;
            }
        }

        return result;
    }

    @Override
    public int read(char[] cbuf, int off, int len) throws IOException {
        if (end) {
            return -1;
        }

        int n = 0;
        int read = 0;
        for (int i=0; i<len && n!=-1; i++) {
            n = this.readNext();
            if (n!=-1 && n!=0) {
                read += 1;
                cbuf[off+i] = (char) n;
            } else if (n==0) {
                i = i-1;
            }
        }

        if (n == -1) {
            end = true;
        }

        return read;
    }

    @Override
    public void close() throws IOException {
        this.in.close();
    }
}

你會這樣做:

Reader yourReader = // open file ...
Reader replaceStart = new ReplaceFilterReader(yourReader,"@@GOTO:\"","");
Reader replaceEnd = new ReplaceFilterReader(replaceStart,"\"@@","");

誠實的回答:使用像Freemarker或Velocity這樣的現有模板庫。

字面答案: Matcher有兩種方法可以用於這種情況, appendReplacement()appendTail() 兩者都要求你使用StringBuffer,這是不幸的,但它是解決這個問題的最優雅的方法。

暫無
暫無

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

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