22

I have a large text file where I want to remove all text between the ^ symbol and the ~ symbol. This needs to work across lines as well.

I tried doing a regular Find and Replace using ^*~ in the Find box and nothing in the Replace box but it found 0 results.

23

This is not possible with a regular Find and Replace. If you use Notepad++ 6, you can take advantage of the new regex engine that supports PCRE (source).

Press Ctrl + H to open the Find and Replace dialog and perform the following action:

Find what:          \^.*?~
Replace with:       
Wrap around:        checked
Regular expression: selected
. matches newline:  checked

Now press Alt + A to replace all occurrences.

The regular expression in Find what is composed as follows:

  • \^ is a literal ^.
  • .*? is the least amount of characters that allows the regular expression to match.
  • ~ is a literal ~.
  • 3
    if you change .*?~ to [^~]*~ then it should work without requiring / using PCRE. – barlop Feb 19 '13 at 17:46
  • 1
    @barlop: I took regular Find and Replace as Search Mode: Normal, not Find and Replace using regular expressions. That might have been a misunderstanding. In any case, if I remember correctly, versions prior to 6 did not support multi-line patterns. – Dennis Feb 19 '13 at 17:53
  • The .*? pattern worked for me to search between characters of ; and <. So the search is FIND ;.*?< REPLACE ; with the options selected for Regular expression and . matches new line – SherlockSpreadsheets Feb 12 '19 at 16:35
8

You're gonna want to search for \^.*?~ and make sure . matches newline is enabled:

enter image description here

This is because ^ has a special meaning, it matches the beginning of a line. Thus, we need to escape it with a backslash \^.

Writing ^* would match "any number of start-of-line in a row". .* matches "any character", but by default it doesn't match newlines.

  • 2
    Be careful with the regex's greedy behavior. If there are multiple ~ characters (or ie. multiple matches) this will match the first ^ all the way to the last ~. – heavyd Feb 19 '13 at 17:41
  • @heavyd <question>Are you sure?</question>. He has used *? that is not greedy it's lazy .*?~ will never match more than one ~. So it's ok. – barlop Feb 19 '13 at 23:12
  • @barlop: He commented shortly after I answered. It was a ninja-edit. – Der Hochstapler Feb 19 '13 at 23:30
2

Try using this regex in the find section \^[^~]*~ to replace everything between ^ and ~ inclusively.

  • and for exclusively, then I suppose that requires PCRE, then do something (?<=^)[^~]*(?=~) I don't have n++ to test it, butif that fails try changing (?<=^) to (?<=\^) – barlop Feb 19 '13 at 17:54
  • For exclusively, just replace with ^~. – Dennis Feb 19 '13 at 17:55
  • @Dennis I mean if he wants to replace everything between the ^ and ~ but not including the ^ and ~ – barlop Feb 19 '13 at 17:56
  • 1
    Isn't that what you get if you replace \^[^~]*~ with ^~? – Dennis Feb 19 '13 at 17:57
  • @Dennis oh I see what you mean, good point. – barlop Feb 19 '13 at 17:58
0

I had a similar BETWEEN replacement requirement with start and end characters. This came from outlook email; hundereds of members and I just wanted the person names with everything removed between <RemoveTextHere>.

Original

First1 Last1 <First1.Last1@email.com>; First2 Last2 <First2.Last2@email.com>; First3 Last3 <First3.Last3@email.com>; First4 Last4 <First4.Last4@email.com>;

Dennis was close to my solution Find what: \^.*?~ but I had to change my literal characters Find what: \<.*?> then I had to remove the first slash Find what: <.*?>.

Updated Find what: <.*?>; Replace empty.

First1 Last1 ; First2 Last2 ; First3 Last3 ; First4 Last4

_

Special Characters: *, $, \, +, ^, ( and ), [, ], { and }

Special Characters are described by Jerry Jeremiah here https://stackoverflow.com/questions/37742519/notepad-wildcard .

  • That doesn't answer the question. – Toto Aug 4 '18 at 9:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.