Help with Regex Replace please

Hi clever people :slight_smile:

I need to manipulate a string. Several times during the string, I have the following text: <time datetime=ā€œ2019-11-06ā€ />
What I need to do it strip out everything except the date. I started with two Regex.Replace - one for "<time datetime=\" and one for \" />. However, that causes problems, as \" /> appears elsewhere in the string. I only want to remove it where there is a date wrapped in . If anyone is able to help Iā€™d be grateful.

Examples:
<p><time datetime=\"2019-11-06\" /> </p></td><td><p /> should result in <p>2019-11-06<p />
<td><p><time datetime=\"2020-06-02\" /><strong> should result in <td><p>2020-06-02<strong>
<col style=\"width: 172.0px;\" /> would not be changed.

Hi,

Can you try the following expression?

System.Text.RegularExpressions.Regex.Replace(text,"<time\s*datetime=\""(\d{4}-\d{2}-\d{2})\""\s*/>\s*","$1")

Regards,

That was perfect. And elegant! Thank you for your help. :checkered_flag:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.