Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用于搜索和替换大文件中的文本的正则表达式_Java_Regex_Io_Nio_Text Search - Fatal编程技术网

Java 用于搜索和替换大文件中的文本的正则表达式

Java 用于搜索和替换大文件中的文本的正则表达式,java,regex,io,nio,text-search,Java,Regex,Io,Nio,Text Search,我正在一个大文件中搜索多行模式,如果发现需要替换内容。我想以一种节省内存的方式来实现这一点。我当前的实现从文件中读取4096字节的文本。然后应用regex search replace并将结果保存在bufferoutputstream中。通过不在内存中加载整个文件,这确实给了我一些内存改进,但是我使用map/flush调用进行了大量IO。需要进一步改进我的代码的建议。此外,如果要搜索的模式被划分为相邻的块,则algo将失败。任何关于如何有效搜索的想法都可以替换被分割成相邻块的文本。假设:要搜索的

我正在一个大文件中搜索多行模式,如果发现需要替换内容。我想以一种节省内存的方式来实现这一点。我当前的实现从文件中读取4096字节的文本。然后应用regex search replace并将结果保存在bufferoutputstream中。通过不在内存中加载整个文件,这确实给了我一些内存改进,但是我使用map/flush调用进行了大量IO。需要进一步改进我的代码的建议。此外,如果要搜索的模式被划分为相邻的块,则algo将失败。任何关于如何有效搜索的想法都可以替换被分割成相邻块的文本。假设:要搜索的文本始终小于4096字节

public void searchAndReplace (String inputFilePath, String outputFilePath) {

    Pattern HEADER_PATTERN =  Pattern.compile("<a [^>]*>[^(</a>)]*</a>", Pattern.DOTALL);
    Charset UTF8 = Charset.forName("UTF-8");
    File outputFile = new File(outputfilepath);
    if (!outputFile.exists()) {
        outputFile.createNewFile();
    }

    FileInputStream inputStream = new FileInputStream(new File(inputfilepath));
    FileOutputStream outputStream = new FileOutputStream(outputFile);

    FileChannel inputChannel = inputStream.getChannel();

    final long length = inputChannel.size();
    long pos = 0;
    while (pos < length) {
        int remaining = (int)(length - pos) > 4096 ? 4096 : (int)(length - pos);
        MappedByteBuffer map = inputChannel.map(FileChannel.MapMode.READ_ONLY, pos, remaining);
        CharBuffer cbuf = UTF8.newDecoder().decode(map);
        Matcher matcher = HEADER_PATTERN.matcher(cbuf);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "Some text");
        }
        matcher.appendTail(sb);
        outputStream.write(sb.toString().getBytes());
        outputStream.flush();
        pos = pos + 4096;
    }

    inputStream.close();
    outputStream.close(); 
}
public void searchAndReplace(字符串inputFilePath,字符串outputFilePath){
模式头\u Pattern=Pattern.compile(“]*>[^()]*”,Pattern.DOTALL);
字符集UTF8=Charset.forName(“UTF-8”);
File outputFile=新文件(outputfilepath);
如果(!outputFile.exists()){
outputFile.createNewFile();
}
FileInputStream inputStream=新FileInputStream(新文件(inputfilepath));
FileOutputStream outputStream=新的FileOutputStream(outputFile);
FileChannel inputChannel=inputStream.getChannel();
最终长长度=inputChannel.size();
长pos=0;
while(pos4096?4096:(整数)(长度-位置);
MappedByteBuffer map=inputChannel.map(FileChannel.MapMode.READ_ONLY,pos,剩余);
CharBuffer cbuf=UTF8.newDecoder().decode(map);
Matcher Matcher=标头_PATTERN.Matcher(cbuf);
StringBuffer sb=新的StringBuffer();
while(matcher.find()){
(某人,“一些文字”);
}
(某人);
write(sb.toString().getBytes());
outputStream.flush();
pos=pos+4096;
}
inputStream.close();
outputStream.close();
}

声明字符串中不太可能出现的特殊字符列表。然后测试字符串,确保其中没有一个特殊字符。在要执行正则表达式的区域之间植入特殊字符。然后,您可以使用/myRegExHere[^\\\]/g进行查找/替换或搜索,您必须使用java吗?如果不是,你使用什么操作系统?你更喜欢使用java,因为我希望应用程序独立于操作系统。如果没有其他替代方法,则希望使用OS grep/find。非常确定您的正则表达式没有执行您认为它可以执行的操作:它要求锚点不包含任何字符
(,或)
。我想你真的想要
“]*>.*?
也许你应该使用流式XML解析器来处理这个问题。+邓肯:是的,我最后也这么做了。谢谢