I have a string like:
[a b="c" d="e"]Some multi line text[/a]
Now the part d="e" is optional. I want to convert such type of string into:
Some multi line text
The values of a b and d are constant, so I don't need to catch them. I just need the values of c, e and the text between the tags and create an equivalent xml based expression. So how to do that, because there is some optional part also.
解决方案
For HTML tags, please use HTML parser.
For [a][/a], you can do like following
Match m=Regex.Match(@"[a b=""c"" d=""e""]Some multi line text[/a]",
@"\[a b=""([^""]+)"" d=""([^""]+)""\](.*?)\[/a\]",
RegexOptions.Multiline);
m.Groups[1].Value
"c"
m.Groups[2].Value
"e"
m.Groups[3].Value
"Some multi line text"
Here is Regex.Replace (I am not that prefer though)
string inputStr = @"[a b=""[[[[c]]]]"" d=""e[]""]Some multi line text[/a]";
string resultStr=Regex.Replace(inputStr,
@"\[a( b=""[^""]+"")( d=""[^""]+"")?\](.*?)\[/a\]",
@"$3",
RegexOptions.Multiline);