tags:

views:

73

answers:

1

What should be the regex for replacing a tr tag with a th tag for the table tag anywhere in an HTML file ?

+3  A: 
Regex.Replace(html, @"(?<=<(/)?)th(?=[\s>])", "tr", RegexOptions.IgnoreCase);
Diadistis
+1. Beat me to it. You'll also need to replace the closing tags: Regex.Replace(strHTML, @"</th(?=[\s>])", "</tr", RegexOptions.IgnoreCase);
Ardman
Yes, I realized just after posting ;) thanks
Diadistis
@Diadistis,Will it replace all the `th` tags anywhere there is `tr` tag in the html file whether there is attributes or anything ?
Harikrishna
new Regex("(<(?:/){0,1})(th)( |>)", RegexOptions.IgnoreCase | RegexOptions.MultipleLine).Replace(html, m => m.Group[1].Value + "tr" + m.Group[3].Value);Not tested...
ccppjava
@Harikrishna: yes
Diadistis
@Diadistis,Sorry my mistake but I want to replace `tr` tag instead of `th` tag.
Harikrishna
@Harikrishna: just swap them : Regex.Replace(html, @"(?<=<(/)?)tr(?=[\s>])", "th", RegexOptions.IgnoreCase);
Diadistis
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
@Diadistis,Will it just replace `tr` OR `<tr>` means with `<` and `>` ?
Harikrishna
@Harikrishna: it will replace `tr` when found in `<tr` or `</tr`
Diadistis
@Diadistis,Thank you very much..
Harikrishna