F
Florian Kaufmann
Simplified to the basics I have the following problem: I want to
replace occurrences of double or single quoted strings in a text file.
The following shows what I want to get.
$ cat MyFile
foo "D'Quote" bar 'S"Qu
ote'
$ MyScript < MyFile
foo \begin{YasDQ}"D'Quote"\end{YasDQ} bar \begin{YasSQ}'S"Qu
ote'\end{YasSQ}
As you can see, the quotes have now new strings (in this example latex
commands) around them. Also note that a single quote within a double
quote is not a meta character anymore, and vice versa. Newlines must
not be treated special.
That's what I tried: It basically searches the next quote. From the
found position onwards it replaces the text up to the next matching
quote. It doesn't work however. I assume mainly because s/// does not
change pos($_) as m// does. I think I just need to know how to
accomplish that after something like s/regex/replacement/ pos points
to the end of the just replaced text.
$ cat MyScript
#!/bin/perl -w
local $\;
local $_ = <STDIN>;
while ( /(?=(["']))/xg ) {
$C = $1;
$Type = $C eq '"' ? 'DQ' : 'SQ';
s<\G($C.*?$C)>
<\\begin\{Yas$Type\}$1\\end\{Yas$Type\}>m;
}
print $_;
replace occurrences of double or single quoted strings in a text file.
The following shows what I want to get.
$ cat MyFile
foo "D'Quote" bar 'S"Qu
ote'
$ MyScript < MyFile
foo \begin{YasDQ}"D'Quote"\end{YasDQ} bar \begin{YasSQ}'S"Qu
ote'\end{YasSQ}
As you can see, the quotes have now new strings (in this example latex
commands) around them. Also note that a single quote within a double
quote is not a meta character anymore, and vice versa. Newlines must
not be treated special.
That's what I tried: It basically searches the next quote. From the
found position onwards it replaces the text up to the next matching
quote. It doesn't work however. I assume mainly because s/// does not
change pos($_) as m// does. I think I just need to know how to
accomplish that after something like s/regex/replacement/ pos points
to the end of the just replaced text.
$ cat MyScript
#!/bin/perl -w
local $\;
local $_ = <STDIN>;
while ( /(?=(["']))/xg ) {
$C = $1;
$Type = $C eq '"' ? 'DQ' : 'SQ';
s<\G($C.*?$C)>
<\\begin\{Yas$Type\}$1\\end\{Yas$Type\}>m;
}
print $_;