Notepad++ find and replace expression help

3

3

I want to find all content between two characters, say A and B:

Asd;lfksjd;fsdfjs;ldfkB which would be sd;lfksjd;fsdfjs;ldfk

and replace them. How would I write this expression?

meiryo

Posted 2011-04-09T07:46:51.940

Reputation: 723

Answers

2

You can match any character between A and B with the following RegEx:

(?<=A).*(?=B)

This doesn't return A or B as part of the matched characters.

  • (?<=A) means that A comes before the main expression and that if it matches you don't want it included in the main result.

  • .* means match any character that occurs 0 or more times. If you want at least a single character to be between A and B you can use .+ instead.

  • (?=B) means that B comes after the main expression and that if it matches you don't want it included in the main result.

Edit:

Notepad++ does not support lookahead/behind, so you can replace

(A).*(B)

with

\1\2

instead.

Note that this is greedy, so if have "AxB y AzB", you'll get "AB". To get "AB y AB", use

(A).*?(B)

instead.

*Edited non-greedy, .?* was incorrect

Gaff

Posted 2011-04-09T07:46:51.940

Reputation: 16 863

I tried it out and it doesn't match anything. Are you sure its not supposed to be this: [?<=A].*[?=B] in which case it would include A and B in the match. – James T – 2011-04-09T08:33:23.910

1My bad -- it's a valid regular expression but it seems Notepad++ doesn't support lookaheads so this won't work in Notepad++. It's not my main editor so I wasn't aware of the missing support. – Gaff – 2011-04-09T08:41:27.190

2

I'm very new to regular expressions so hopefully this works for you.

I think A.*B would be the regular expression to search for. That is, search for A and B with zero or more things between them.

This regular expression includes the A and B in the match so you'd have to add them back in with the replace.

AreplaceB Would be the text to replace it with.

enter image description here

James T

Posted 2011-04-09T07:46:51.940

Reputation: 8 515

It's a simple fix though. I think your solution might be better, James answered first so I chose it. – meiryo – 2011-04-09T08:32:55.613