Multi-line regex search in whole file

前端 未结 6 984
悲哀的现实
悲哀的现实 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:12

    Regex is not the way to go, especially not with these large amounts of text. Create a little parser of your own:

    • read the file line by line;
    • for each line:
      • loop through the line char by char keeping track of any opening/closing string literals
      • when you encounter '/*' (and you're not 'inside' a string), store that offset number and loop until you encounter the first '*/' and store that number as well

    That will give you all the starting- and closing-offset numbers of the comment blocks. You should now be able to replace them by creating a temp-file and writing the text from the original file to the temp file (and writing something else if you're inside a comment block of course).

    Edit: source files of 2GiB??

提交回复
热议问题