Friday, 2 January 2015

Regular Expression Find and Replace in Notepad++

So today I found myself editing a page from our internal Wiki and the formatting was completely and utterly up: Headings were all wrong, some at level 2, others at level 3 and quite a few other things were wrong as well.

I copied and pasted the page to Notepad++ and decided to use Find and Replace, but how could I use Regular Expressions to find and replace.

Simple, it turns out that all that's needed is to use capturing groups.

Input Text:
1. == Section 1 ==
....
2. == Section 2 ==
....
3. === Section 3 ===
....
4. == Section 4 ==
Desired Output Text:
1. === Section 1 ===
....
2. === Section 2 ===
....
3. === Section 3 ===
....
4. === Section 4 ===
So this is the regular expression I used:
Find Regular Expression Explanation:
Replace Regular Expression Explanation:

Add = to the end of each capturing group

Hope this helps

1 comment:

  1. I use them for things like this in powershell when im feeling lazy :)

    Get-Content Awesome.dll.config ) | Foreach-Object {
    $_ -ireplace "(?<=data source=)(.*)(?=;initial catalog)" , $sqlserver `
    -ireplace "(?<=initial catalog=)(.*)(?=;integrated security)" , $sqldbase
    } | Set-Content -Encoding UTF8 -Path Awesome.dll.config

    SteveC / AMS

    ReplyDelete