c# - Pattern Replacement using regular expression -
i want replace [cite:lamport94] \cite{lamport94} using regular expression in c#.
string input = "how easy create structured document within [cite:lamport94]. should able see." string output = "how easy create structured document within \cite{lamport94}. should able see."
here code can use. capture alphanumeric string inside square brackets ":" delimiter:
var input = "how easy create structured document within [cite:lamport94]. should able see."; var rxreg = new regex(@"\[(\w+):(\w+)]"); var result = rxreg.replace(input, @"\$1{$2}"); output:
how easy create structured document within \cite{lamport94}. should able see. btw, if have more [a-za-z0-9_] characters inside brackets, can extend \w [\w.:-] (the pattern prefer match mm.xxx, or xx-yy, or xx:yyyy strings. add more if need (mind hyphen must stay @ end of character class stay literal hyphen).
to limit cite, use instead of first \w:
var rxreg = new regex(@"\[cite:(\w+)]"); var result = rxreg.replace(input, @"\cite{$1}");
Comments
Post a Comment