Monday, September 17, 2012

NotePad++, Regular Expressions and Replacement using found text

Scenario:

You have the following text and you want to copy the values in old to new

<UserMapping old="abc\dfs" new="" />
<UserMapping old="abc\adfad" new="" />
<UserMapping old="abc\sfsafsd" new="" />
<UserMapping old="abc\jdjfgg" new="" />

First you need to figure out the regex to match what you want. Here is what I have:

old="([A-Za-z\\]+)".*$

What you need to notice is that the part of the string that I want to match is inside parenthesis ( () ). This allows us to use the value in the replacement. The regex will match starting from old and end at the end of line. During the match a group will be created from whatever is in the parenthesis, in this case it will include all alphabets (upper and lower case), as well as the slash.

For the replacement, we will use the following string:

old="\1" new="\1" />

Here, notice that I use \1 for the old and new. The matched group value will be used for the old and new values.

No comments:

Post a Comment

Remember, if you want me to respond to your comment, then you need to use a Google/OpenID account to leave the comment.