1

I'm making a C# regex to find and replace patterns related to html content. i need to get all the stuff like that:

<table border=0 align=center id=mytable5>

corrected like that:

<table border="0" align="center" id="mytable5">

i tried out this:

String pattern = @"\s(?<element>[a-z])=(?<valeur>\d+?[a-z])\s?[\>]";
String replacePattern = "${element}=[\"]${valeur}[\"]";
html = Regex.Replace(html, pattern, replacePattern, RegexOptions.IgnoreCase);

but there is absolutly no effect. Any help would be greatly appreciated. thank you all


Actually King King, there is a problem with your regex

<table border=0 align="center" id="mytable5">

will give

<table border="0" align=""center"" id=""mytable5"">

thats why the regex must check this

[a space][a-z]=[a-z0-9][a space or '>']

nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Jay Cohen
  • 193
  • 2
  • 9
  • http://stackoverflow.com/a/1732454/393487 – Ahmed KRAIEM Aug 29 '13 at 08:14
  • 1
    its not about making the regex understand html here, but its just a pattern remplacement in a string, nothing more. but thanks for your link btw, i like how the letters are leaking at the bottom, nice visual effect – Jay Cohen Aug 29 '13 at 09:58

2 Answers2

0
var html = "<table border=0 align=center id=mytable5>";
html = Regex.Replace(html, @"=\s*(\S+?)([ >])", "=\"${1}\"${2}", RegexOptions.IgnoreCase);
King King
  • 61,710
  • 16
  • 105
  • 130
0

I got it

String pattern = @"([a-z]+)=([a-z0-9_-]+)([ >])";
String replacePattern = "${1}=\"${2}\"${3}";
html = Regex.Replace(html, pattern, replacePattern, RegexOptions.IgnoreCase);

will get

<table border=0 align="center" id="mytable5">

corrected to this:

<table border="0" align="center" id="mytable5">

thanks for King King that showed me the path

Jay Cohen
  • 193
  • 2
  • 9