Geeks With Blogs
kazim mehdi

I have to replace align=”right” to align=”left” of all <td> in all files in the whole project. Consider the following html

<table>
<tr>
<td align="right" style="border: 1 solid black">
cell1
</td>
<td style="border: 1 solid black" align="right">
cell2
</td>
<td id="td01" align="right" style="border: 1 solid black">
cell3
</tr>
</table>

 

Work breakdown:

  1. How to replace in single file
  2. how to replace in multiple files

 

How to replace in single file

if you look carefully “align” tag is placed at different places. 

To replace align=”right” to align=”left” only possible way is by use of Regular expression.

I need to take care that it should replace only the align attribute that is used in td tag.

To develop and test  the regular expression I used the very good tool called Expresso its free by the way.

To make sure that only td’s align attribute’s value is replaced I have to use regular expression technique called "Look behind” a very good source of learning regular expression is Regular-Expression.info ( I am using it a reference from a long time).

What Look behind do is: it look behind the matching expression.

e.g

((?<=\<td(.*))align\="right")

In the above regular expression (?<=\<td(.*)) is look behind part.

Expression search for the align tag and then look behind and filtered out the matches where look behind satisfied. In the similar way we also have “Look Ahead”.

If it is still not clear why we need to go into look behind fuss is because if we use only this (align\="right") it will replace the value of all the align attributes of all the tags. And if we use ((\<td(.*))align\="right") it will find the right align attribute but when we try replace it will replace the whole tag line instead of just replacing the align attribute value.

If you still have doubts try yourself in Expresso using the sample text provided in the start.

How to replace in multiple files

After I got the tested regular expression in hand I tried Visual studio 2010 find and replace with setting “Use” to Regular expression but it didn’t produce any result.

Then I tried the same in notepad++ that also a great tool in my arsenal. It didn’t able to handle Look behind construct.

My next choice then GrepWin Regular expression based find and replace application. It also don’t know how to handle Look behind construct.

Then I left with only two choices either develop a console application myself or use powershell so I opt the later one.

below in the full powershell code to find and replace the above mentioned goal.

# need to be changed 

$path="C:\dev\pages"

$oldValue="((?<=\<td(.*))align\=""right"")"
$newValue="align=""left"""
# need to be changed

Write-Host "Searching for : $oldValue"
Write-Host "Replaced value: $newValue"


$count=0
dir $path -Recurse -Include *.aspx, *.ascx| ForEach-Object{
$text=Get-Content $_.FullName;
if ($text -match $oldValue)
{
$text=$text -replace $oldValue,$newValue
set-content $_.FullName -value $text

$count++
}
}

Write-Host "total files found:$count"
Posted on Friday, April 1, 2011 3:24 PM Powershell , Regular expression | Back to top


Comments on this post: Replacing an attribute value of a specific html tag in multiple files

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © kazim sardar mehdi | Powered by: GeeksWithBlogs.net