Friday, September 13, 2013

Notepad++: Use a Regex Match In The Replace Text

It seems like I'm always trying to remember how to use a regex match in the replaced text in Notepad++ so this time I'm writing it down.

First of all let's say we have some XML text
<app_score><xsl:value-of select="app_score" /></app_score>
<tr_weight><xsl:value-of select="tr_weight" /></tr_weight>
<rearend><xsl:value-of select="rearend" /></rearend>

And, now lets say we want to use the element tag to transform it to something like:
<xsl:template match="app_score" />
<xsl:template match="tr_weight" />
<xsl:template match="rearend" />


You can see the match attribute comes from the element name so here is how you can do that.

1) First we build our regex with matching...now for matching we surround the piece we want to match with parenthesis() and because we want to transform the entire line we need the regex to match the whole line. This is what I came up with for this example: <([^\s]+)>(.*)</\1>

There are a couple cool things to note here. First, we actually are matching two groups: inside the element([^\s]+) and the element's text(.*). Second, we're using the first match for the closing tag(</\1>).

2) Now we know how the matching works it's easy to insert the match into our replace text, like so: <xsl:template match="\1" /> where the \1 is the first matching group. We could just as easily use the \2 for the second matching group.


1 comment:


  1. 0
    down vote
    favorite
    I have a folder with 2 kind of sentences/rows. One that has < h1 > tag, and other doesn't. Like so:

    < td class="articles">Mama< /td>

    < td class="articles">< h1>Tata < /h1>< /td>
    I want to find with notepad++ only the rows that doesn't have the word < h1>

    Does anybody know how to do this?

    ReplyDelete