Multiline parenthesis matching, with e.g. SED script, in LaTeX doc

In a LaTeX manuscript, I need to replace many occurrences of

\emph{some string}

with some string, i.e. whatever string is inside. The string inside often may extend over several lines, and there may be other occurences of curly brackets inside it. So for example

\emph{this \it{is} a
test} 

should be this

this \it{is} a
test

Any good ideas?

Sune

so from this sample,

\emph{this \it{is} a
test}

what's your expect output?

[1]
So basically you're saying that given

\emph{ alpha }

where alpha is any arbitrary string of characters (including newlines), you wish to replace it with

alpha

correct?

[2]
Do you know if the \emph tags are nested? If they are nested then that would require a pushdown-automata, as regular expressions alone (i.e., finite automatons) are insufficient to parse balanced-items (such as balanced parenthetical statements, for example).

rdcwayx: That would be

this \it{is} a
test

---------- Post updated at 12:13 AM ---------- Previous update was at 12:11 AM ----------

Hi Mubby,
Sorry didn't see your second question. So for completeness:
1) Yes, you got the idea.

2) No, there are no nested occurences of emph! (But there may be curly brackets in alpha).
Sune

---------- Post updated at 03:06 AM ---------- Previous update was at 12:13 AM ----------

Found a piece of (recursive) code and have modified it:

:top
/\\emph{.*}/ {
s/\\emph{\([^{}]*\)}/\1/g
t top
}
/{/{
        N
        b top
        }

Doesn't work however, but I have a feeling it just needs a tiny modification .....??

Assuming all the curly braces are balanced:

perl -0ne 's/\\emph{//g;$i=0;while(/./gs){$i-- if $& eq "{";$i++ if $& eq "}"; if ($i<1){print $&}else{$i=0}}' infile > outfile
1 Like

Worked like a charm!
Thanks!
Sune