4

Here is my text:

<span class="c1">Testing "this string"</span>

and I want to end up with this:

<span>Testing "this string"</span>

so I tried to use this regex in Notepad++ to replace with nothing:

 class=".*"

but that matches this:

 class="c1">Testing "this string"

How do I stop that match after one instance of "?

10
  • where are you doing this? on server using server-side code or in the browser? Commented Aug 27, 2010 at 20:42
  • @Scott: OP is asking how to replace it using Notepad++. I've tagged and edited the title accordingly. Commented Aug 27, 2010 at 20:43
  • This summarizes my feelings about HTML and regex very well :-) - stackoverflow.com/questions/1732348/… Commented Aug 27, 2010 at 20:47
  • 1
    @Franci that's my go-to link for this sort of questions usually too, but in this case the OP wants to do find and replace within their IDE, so it's a legitimate exception ;) Commented Aug 27, 2010 at 20:50
  • 1
    @BoltClock potato, potato (I guess that doesn't really work in text). :) Commented Aug 27, 2010 at 20:56

2 Answers 2

15

By default, regular expressions are greedy (and so .* will match as much as it possibly can, which, in your case is c1">Testing "this string). In general, you have two ways of getting around this:

  1. Use a nongreedy (or lazy) modifier (.*?), which will match as little as possible (in your case, just c1). Notepad++ doesn't support lazy modifiers, though.
  2. Specify exactly what you want to match with class="[^"]*", which will match everything that isn't a quote. In general, this is the more optimized solution, as well.
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the complementary character class. (I'll do it once my vote limit resets though.)
Notepad++ didn't work with the ?, but the second solution was perfect. Thanks!!
@Khan: as BoltClock mentioned below, Notepad++ doesn't support ungreedy modifiers.
1
class=".*?"

Will make the * lazy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.