regex in notepad++

Searching a string using the ‘Find‘ or ‘Find & Replace‘ function in text editors highlights the relevant match (e.g. searching ‘le‘ highlights it inside words such as ‘apple‘, ‘please’ etc). However, some advanced editors such as Notepad++ (I mention Notepad++ in my examples since its my favourite so far!) supports the use of regex, which recently saved me hours of manually replacing strings and numeric values in files containing HTML and JacaScript codes.


Regex characters can be used to create advanced matching criteria. The following table introduces some of them with practical examples. But before starting make sure that you change the Search Mode from Normal to Regular expression in your Find or Find & Replace dialogue box.

  • [ ]
  • The square brackets can be used to match ONE of multiple characters. For instance, [abc] matches any of the characters a, b or c. Hence, b[eo]n will match words like ben and bon, but not been or beon. Ranges can also be used, [a-z] is any lower case character and so on.

  • ^
  • The caret can be used inside the square brackets to exclude characters from the match. For instance, hell[^o] means the string ‘hell’ will be ignored if followed by the letter ‘o’. Another example is [^A-Za-z] which will exclude all alphabetic characters.
    However, if not placed inside a set, ^ can be used to matches the start of a line.

  • $
  • This matches the end of a line.

  • .
  • The period or dot matches any character.

  • \d
  • Matches any single digit.

  • \w
  • Matches any single alphanumeric characters or underscore.

  • \s
  • Matches whitespaces including tabs and line breaks.

  • *
  • The asterisk or star sign matches 0 or more times. For example, Ba*m matches Bm , Bam , Baam etc.

  • +
  • The plus sign matches 1 or more times. For example, lo+l matches lol , lool , loool etc.

  • \<
  • Matches the start of a word. For example, \< directly followed by 'sh' matches 'she' but does not matches 'wish'.

  • \>
  • Matches the end of a word. For example, sh\> matches ‘wish’ and does not matches ‘she’.

  • ( )
  • The round brackets can be used in the Find & Replace function to tag a match. tagged matches can then be used in replace with \1, \2 etc.

    For example, If you write 123xxxRRR in the search and 123\1HHH in the ‘Replace with’ filed, the result will be: 123xxxHHH.

  • \
  • The backslash can be used to escape regex characters. For example to match 1+1=2, the correct regex is 1\+1=2. Otherwise, the plus sign will have a special meaning.

Further, the following two examples should be giving you a better idea of how to use regex in your editor:

  • Find: Win([0-9]+) Replace with: Windows\1
  • Will search for strings like Win2000, Win2003 and changes them to Windows2000, Windows2003…

  • Find: [a-z]+(\d\d)\> Replace with: Windows\1
  • Will search for all alphanumerics followed by 2 digits only at the end such as Win98 and Win07 and changes them to Windows98, Windows07…

24 Responses to “Understanding RegEx with Notepad++”

  • [...] ausgeführt und alles in Ordnung. Hier habe ich interessante Infos [...]

  • Dezi:

    Thanks you very much for spending the time to explain this Notepad++ RegEx function so well & so clearly — a big help!!

  • [...] http://blog.creativeitp.com/posts-and-articles/editors/understanding-regex-with-notepad/ Share this:TwitterFacebookLike this:LikeBe the first to like this. This entry was posted on Monday, September 17th, 2012 at 3:55 pm and posted in RegEx. You can follow any responses to this entry through the RSS 2.0 feed. « Finally Definitely Executes Last [...]

  • Cristian:

    I have a standard Apache log file to filter. The regular expresion that match the entire line is “^(\S+) \S+ \S+ \[([^\]]+)\] “([A-Z]+)[^"]*” \d+ \d+$”, but now I need to select the lines that does not match, I need the “inverse” selection of this regex. How could I search for those lines that not match the regex?

  • Thanks for this micro guide.

    Do you know how to ignore case?

    In egrep UNIX utility just -i parameter:

    egrep -i ‘^(From|Subject|Date):’ emails.txt

    Thanks in advance!

  • Michel Merlin:

    How do I remove all lines starting with “#house:” in Notepad++?
    ~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
    Thanks a lot for these great explanations. However I tried:

    “Find what” #house:(.*)$
    “Replace with” (empty)

    which did remove the *contents of* the lines but left a blank line each time. Any help? TIA,

    Versailles, Fri 30 Nov 2012 19:19:15:00 +0100

    • Anonymous:

      Find What : => ^(#house:)(.*)\r\n
      Replace With :=>

    • Michel Merlin:

      In Regular expression Search Mode, Find and Replace occurs only on part of the file
      ~–~–~–~–~–~–~–~–~–~- ~–~–~–~–~–~–~–~–~–~- ~–~–~–~–~–~–~–~-
      Thanks a lot “Anonymous” Tue 23 Apr 2013 15:33 GMT for your advice: (Find « ^(#house:)(.*)\r\n » and replace with nothing) does remove entirely all lines starting with “#house:” using Notepad++ with “Search Mode: Regular expression”.

      It appears however (at least in my current Windows 7 64bit Ultimate + Notepad++ 6.3.3 UNICODE) that the “Find and Replace” doesn’t work as usual on the whole file, but only from the current cursor to the end of file; so for this to work as expected you need first to type . I only discovered this today, whence my mistaken post of Sun 05 May 2013 12:53 GMT, sorry for that.

      Versailles, Sat 03 Aug 2013 07:47:00 +0200

    • Michel Merlin:

      so for this to work as expected you need first to type “Ctrl+Home”

    • Michel Merlin:

      Does work if «Wrap around» is checked
      ~–~–~–~–~–~–~–~- ~–~–~–~–~
      Sorry again. Posts can’t be edited so I have to add another one. Actually I had incidentally (when debugging something else) cleared (unchecked) the “Wrap around” case in the “Replace” dialog. Once this checked back, the action requested: “Find « ^(#house:)(.*)\r\n » and replace with nothing”

      is duly performed under “Search Mode: Regular expression”

      and NOT under “Search Mode: Extended (\n, \r, \t, , \x…)”

      Versailles, Sun 04 Aug 2013 20:15:00 +0200

  • Jass:

    @John Ortiz:

    John, you can uncheck ‘Match case’ checkbox in the ‘Find’ window to ignore the case while searching.

  • sevenkul:

    A correction: “For example, If you write 123(…)RRR in the search and 123\1HHH in the ‘Replace with’ field, 123xxxRRR will be: 123xxxHHH.”

  • Muhammad Ali:

    Thanks a bunch dude

  • Michel Merlin:

    “Anonymous” 23 Apr 2013 at 15:33 GMT, your « ^(#house:)(.*)\r\n » doesn’t work in my test (Notepad++ 6.3.2 UNICODE, Windows 7 64bit Ultimate), neither in Regular expression nor in Extended modes. Did you really test it?
    Versailles, Sun 05 May 2013 14:53:10 +0200

  • [...] RegEx in Notepad++ also see -blog.creativeitp.com [...]

  • molecule:

    might could add a note about difference between maximal or greedy searches using .* or .+ and minimal searches using .*? or .+?

    given 123a456a then .*a finds 123a456a (the maximal or greedy solution) while .*?a finds only 123a the minimal or first solution

    seems every editor I’ve used has its own special regex code system

  • Hello,
    I am trying to remove duplicate lines from a probing Gcode file. The typical data format would look like this…..

    G0 X -6.5 Y 2.50
    G0 X -6.2207 Y 3.5974
    F3
    G1 X -6.2207 Y 3.5974
    G1 X -6.2207 Y 3.5974
    G1 X -6.2207 Y 3.5974

    with often 100’s of lines that should be removed. Could you suggest a set of characters to help remove these in Notepad++?

    Thank you very much!!

    David

  • Lyla:

    Hey everyone, I am trying to search using this expression, however, it keeps finding each line break. Any idea how to fix that? Thanks!

    A-Za-z0-9~!@#$%\*()+={}:”;’,.?\/&_[-] –

  • Michael:

    This guide is very helpful,
    Thank you,

  • gustonator:

    Hi all, I need help. I’m trying to convert .gpx from Strava to Endomondo, but I’m strugling. I need to insert a after every pause I made which is longer than 10s. ie.:

    convert this:

    237.0
    2014-06-08T07:47:01Z

    235.6
    2014-06-08T07:58:46Z

    to this:

    237.0
    2014-06-08T07:47:01Z

    235.6
    2014-06-08T07:58:46Z

    can you please help me what should be the expression?
    you can reach me on mail gusto@medzev.sk

    MANY THANKS !

  • NMGMarques:

    I have a file with various lines containing coordinates. They are in the following format:
    {pos(192,225)}

    How can I remove said lines? I can’t seem to use the {} chars without running into invalid expressions.

  • Chopin:

    Thanks for this guide.

    There is one non-regex (NP) useful feature you can mentioned in your manual:
    \ search for words with length 6-8.

Leave a Reply

*

Haider’s WebSpace
Welcome to my technical blog. This is where I write, archive and share computer related articles. Subjects vary from posting technical solutions to researching particular topics. Feel free to comment and talk IT!
Sponsored Links
My Tweets