Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
1  
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .