Notepad++ non-greedy regular expressions

RegexNotepad++Regex Greedy

Regex Problem Overview


Does Notepad++ support non-greedy regular expressions?

For example for text:

abcxadc

I want to get parts using pattern:

a.+c

And now I get whole string instead of 2 parts. I tried to use the '?' operator but without success.

Regex Solutions


Solution 1 - Regex

Update: from version 5.9 (build time Mar, 31. 2011), Notepad++ supports non greedy regular expressions (new scintilla 2.5).

Solution 2 - Regex

I did the following with Notepad++ V6.1.5 (It has now PCRE regex engine):

> a.+?c

and got 2 parts (abc and adc)

Lazy(non-greedy) searches are now possible.

Solution 3 - Regex

NOTE: This accepted answer is obsolete as of March 31, 2011. Notepad++ v5.9 and higher now support non-greedy regular expressions.

Please see an updated answer here or here.


Notepad++ doesn't support the lazy ? modifier. Instead, you can specify what you don't want:

a[^c]+c

Which specifies: match a, followed by one or more character that isn't c, followed by c. This will match abc and adc.

Solution 4 - Regex

While cleaning up a logfile of dispensable parts, I had trouble using non-greedy regular expression with "Replace All" and an empty "Replace with" pattern. My solution was to make the pattern match the whole line without changing the rest of the line.

Example: remove every start of line up to the first semicolon: instead of ^.+?: -> now use ^.+?:(.*)$ -> \1 and press "Replace All"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPrzemysław MichalskiView Question on Stackoverflow
Solution 1 - RegexPrzemysław MichalskiView Answer on Stackoverflow
Solution 2 - Regexuser1584660View Answer on Stackoverflow
Solution 3 - RegexDaniel VandersluisView Answer on Stackoverflow
Solution 4 - RegexSebastianHView Answer on Stackoverflow