How to replace only first instance of text in note

2019-02-20 23:20发布

I'm working on updating a website with a few changes and I've managed to do most of the changes to multiple files using standard find and replace searches. There is just one error left over from my f/r that I need to fix but it occurs several times throughout the document and I want to replace only the first instance, the only time it's an error.

 </div><div class="boxb">

That occurs seven times in each file, only the FIRST time I need to remove the trailing, closing DIV. All the others are correct.

I gather I need to use the Regular Expression mode but I've tried a few examples and they either don't work or they still replace all occurences, not just the first as I require.

Also to be clear, I don't know if it makes a difference, but I need to do this to multiple files, so using 'Find In Files' rather than just 'Replace'.

Thanks

2条回答
仙女界的扛把子
2楼-- · 2019-02-20 23:32

The text you give appears to be fixed, so a regular expression is not needed to find it. The replace part of Find in files replaces all occurrences in each file. Assuming the number of files is not huge, I would suggest using the Find all button on Find in files and then manually changing them one by one. If the number of files is large then Notepad++ is probably not be the best tool to make the changes.

For other options consider programs such as Perl or Awk; with these the task would be simple.

查看更多
Deceive 欺骗
3楼-- · 2019-02-20 23:49

One way is to match the whole content of the file, then replace back the parts that you still want to keep. This ensure that the whole file produces exactly 1 match. This method is not yet tested, but it might be slow on big file.

  1. Just write your regex as per normal, then add (.*?) in front and (.*) at the back:

    (.*?)pattern(.*)
    

    You should test with (.*?)pattern on a few files to make sure that it matches the first instance of the text you want to remove.

  2. Check the checkbox ". matches newline".

  3. The replacement string may vary depending on your regex, but the idea is to capture the parts that you want to keep in the regex of step 1 and replace them back in this step.

    For example, if you want to remove anything that matches pattern, then use $1$2 as replacement string.

查看更多
登录 后发表回答