0

I have html table td data with

id="ctlHoursReport_rptrHoursReport_tdMon_29", 
id="ctlHoursReport_rptrHoursReport_tdTue_29", 
id="ctlHoursReport_rptrHoursReport_tdWed_29",

etc. through to Sun. The number at the end is the row number of the table.

Each td represents a day of the week. I'd like to remove the entire id including the day abbreviation.
But I would like to keep the ids of the other tds in the row, eg. id="ctlHoursReport_rptrHoursReport_tdPayRate_29"

How can I put this into a single Regex.replace method?

[edit] This is what I put in so far. It works, but is really clunky:

html = Regex.Replace(html, "id=\"ctlHoursReport_rptrHoursReport_tdMon[^\"]+\"|id=\"ctlHoursReport_rptrHoursReport_tdTue[^\"]+\"|id=\"ctlHoursReport_rptrHoursReport_tdWed[^\"]+\"" +
                                    "|id=\"ctlHoursReport_rptrHoursReport_tdThu[^\"]+\"|id=\"ctlHoursReport_rptrHoursReport_tdFri[^\"]+\"|id=\"ctlHoursReport_rptrHoursReport_tdSat[^\"]+\"" +
                                    "|id=\"ctlHoursReport_rptrHoursReport_tdSun[^\"]+\"", String.Empty, RegexOptions.IgnoreCase);

[2nd edit] The ids I want to remove - days of the week begin with M, T, W, T, F, S, S. The ones I don't want to touch - PayRate, Reg, OT, begin with different letters.

So I was able to reduce the statement to:

html = Regex.Replace(html, "id=\"ctlHoursReport_rptrHoursReport_td[MTWFS].+[0-9]\"", String.Empty, RegexOptions.IgnoreCase);

So, it's cleaner, but what I would like to say is something like:

html = Regex.Replace(html, "id=\"ctlHoursReport_td[Mon,Tue,...Sun]_[0-9]\"" 
Joth
  • 155
  • 1
  • 2
  • 10
  • I edited my question to show what I have so far. – Joth Oct 27 '17 at 00:13
  • Split the string on "_" then apply Regex match to the last part. – Jasen Oct 27 '17 at 00:14
  • Could you give me an example of that? Do you mean the regex string? The id attribute has three '_' and the html string is about a couple meg in size. – Joth Oct 27 '17 at 00:27
  • The regex is arguably the most complex part of the process. You can simplify your regex if you only need to account for the last portion. The rest of the parts are put aside an recombined in the end. – Jasen Oct 27 '17 at 00:31
  • 2
    Also, use a Html parser like Html Agility Pack to find the nodes you want to edit. Then apply a regex to replace its id attribute. – Jasen Oct 27 '17 at 00:41
  • Agree with Jasen, and that comment above is also an explanation of why your code is "clunky". – Wiktor Stribiżew Oct 27 '17 at 07:23

0 Answers0