I'm not sure I understand what problem you are trying to describe -- the sample input string you showed (\caption{*}) turns into the output you want (<caption>*</caption>) using the code snippet as originally posted. Maybe if you showed the particular input sample that causes a problem for you?
Anyway, I'd probably write the regex a little differently -- this would behave a bit different from the OP version, in allowing the input to have an empty string (i.e. nothing) inside the curly braces (whereas the OP would not match unless there's at least one character in there):
s# \\ (caption) \{ ([^}]*) \} #<$1>$2</$1>#gisx;
That also works on the sample input string you originally posted. If you have a string that doesn't work with that, show us. |