tags:

views:

37

answers:

2

I have texts like this one:

this is a text in [lang lang="en" ]english[/lang] or a text in [lang lang="en" ]spanish[/lang]

I need to substitute them for:

this is a text in <span lang="en">english </span> or a text in <span lang="es">spanish</span>

I need a regular expression, not a simple replace. The languages in the lang tag can be whatever.

A: 

You could simply replace [/lang] with </span> and use

Regex.Replace(text, @"\[lang lang=""(\w+)""\]", "<span lang=\"$1\">")

to replace the opening tag.

Jens
Thx.The final solution has been: string result = Regex.Replace(str, @"\[lang lang=(.*)\](.*?)\[/lang\]", "<span lang=$1>$2</span>");I haven't found the way to escape \" inside this regex, don't know.
netadictos
@netadictos: My mistake, sorry. In verbatim strings quotes are escaped with more quotes, not backslashes.
Jens
+1  A: 

Regex:

\[lang lang="([^"]*)" \](.*?)\[/lang\]

Replacement:

<span lang="$1">$2 </span> 
codaddict
The space at the ending looks weird. \[lang lang=\"([^\"]*)\"\s*\](.*?)\[/lang\]
Bird