This is html:
<font color="#000fff" size="1" face="Arial">Genuine Windows® 7 Home Premium (64-bit)</font>
This is what I wanted to convert:
[color="#000fff"]Genuine Windows® 7 Home Premium (64-bit)[/color]
This is what I tried:
var post = Regex.Replace(post, "<font color=\"([a-fA-F0-9\\#]+)\">(.*?)</font>",
m => "[color=\"" + m.Groups[1].Value + "\"]" + m.Groups[2].Value + "[/color]");
Its not matching.
No, because you haven't catered for the size="1" face="Arial"
. Try this:
"<font color=\"([a-fA-F0-9\\#]+)\"[^>]*>(.*?)</font>"
(Note the addition of [^>]*
to capture everything else in the opening font tag)
Your fix is:
using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main()
{
string post = "<font color=\"#000fff\" size=\"1\" face=\"Arial\">Genuine Windows® 7 Home Premium (64-bit)</font>";
post = Regex.Replace(post, "<font color=\"([a-fA-F0-9\\#]+)\"[^>]*>(.*?)</font>",
m => "[color=\"" + m.Groups[1].Value + "\"]" + m.Groups[2].Value + "[/color]");
Console.WriteLine(post);
}
}
Test this code here .
Update: Performance bottlenecks fixed. See it in action .
Find:
<font.*?color="([^"]*)"[^>]*>([^<]*)</font>
Note: This assumes color attribute will definitely exist.
Replace:
[color=$1]$2[/color]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.