Multi-line regex search in whole file

前端 未结 6 985
悲哀的现实
悲哀的现实 2021-02-15 16:47

I\'ve found loads of examples on to to replace text in files using regex. However it all boils down to two versions:
1. Iterate over all lines in the file and apply regex to

6条回答
  •  执念已碎
    2021-02-15 17:11

    Here's the Answer:
    There is no easy way

    I found a StreamRegex-Class which could be able to do what I am looking for.
    From what I could grasp of the algorithm:

    • Start at the beginning of the file with an empty buffer
    • do (
      • add a chunk of the file to the buffer
      • if there is a match in the buffer
        • mark the match
        • drop all data which appeared before the end of the match from the buffer
    • ) while there is still something of the file left

    That way it is not nessesary to load the full file -- or at least the chances of loading the full file in memory are reduced...
    However: Worst case is that there is no match in the whole file - in this case the full file will be loaded into memory.

提交回复
热议问题