Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
    
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 Apr 9 '11 at 8:33
    
My 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. –  8088 Apr 9 '11 at 8:41

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

share|improve this answer
    
+1 for screenshot. Works like a charm, thanks! –  meiryo Apr 9 '11 at 8:30
    
It's a simple fix though. I think your solution might be better, James answered first so I chose it. –  meiryo Apr 9 '11 at 8:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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