What should be the regex for replacing a tr
tag with a th
tag for the table
tag anywhere in an HTML file ?
views:
73answers:
1
+3
A:
Regex.Replace(html, @"(?<=<(/)?)th(?=[\s>])", "tr", RegexOptions.IgnoreCase);
Diadistis
2010-05-21 08:58:49
+1. Beat me to it. You'll also need to replace the closing tags: Regex.Replace(strHTML, @"</th(?=[\s>])", "</tr", RegexOptions.IgnoreCase);
Ardman
2010-05-21 09:01:47
Yes, I realized just after posting ;) thanks
Diadistis
2010-05-21 09:03:02
@Diadistis,Will it replace all the `th` tags anywhere there is `tr` tag in the html file whether there is attributes or anything ?
Harikrishna
2010-05-21 09:06:36
new Regex("(<(?:/){0,1})(th)( |>)", RegexOptions.IgnoreCase | RegexOptions.MultipleLine).Replace(html, m => m.Group[1].Value + "tr" + m.Group[3].Value);Not tested...
ccppjava
2010-05-21 09:15:55
@Harikrishna: yes
Diadistis
2010-05-21 09:18:06
@Diadistis,Sorry my mistake but I want to replace `tr` tag instead of `th` tag.
Harikrishna
2010-05-21 09:24:26
@Harikrishna: just swap them : Regex.Replace(html, @"(?<=<(/)?)tr(?=[\s>])", "th", RegexOptions.IgnoreCase);
Diadistis
2010-05-21 09:26:22
Could be shortened: `@"(?<=<(/)?)tr\b"`; also I don't know if whitespace might occur between `<`, `/` and `tr` - if so, `@"(?<=<\s*(/)?\s*)tr\b"` might be safer.
Tim Pietzcker
2010-05-21 09:26:24
@Diadistis,Will it just replace `tr` OR `<tr>` means with `<` and `>` ?
Harikrishna
2010-05-21 09:52:28
@Harikrishna: it will replace `tr` when found in `<tr` or `</tr`
Diadistis
2010-05-21 10:23:33
@Diadistis,Thank you very much..
Harikrishna
2010-05-21 10:27:29