Multiline substitution

G

gooliver

I've this text on a TeX-file source.tex:

bla bla bla...
$$
some equation
$$
bla bla bla...

and i want convert source.tex in a file dest.tex where $$
are replaced from the couple \[...\]

bla bla bla...
\[
some equation
\]
bla bla bla...

but i've encountered most difficulties about pattern-matching
of the multiline string $$...$$ !!!

Have you some suggestion?
 
L

Lars Haugseth

* gooliver said:
I've this text on a TeX-file source.tex:

bla bla bla...
$$
some equation
$$
bla bla bla...

and i want convert source.tex in a file dest.tex where $$
are replaced from the couple \[...\]

bla bla bla...
\[
some equation
\]
bla bla bla...

but i've encountered most difficulties about pattern-matching
of the multiline string $$...$$ !!!

Have you some suggestion?

Assuming two $ characters never appear next to each other in
other circumstances:

$code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
 
G

gooliver

Assuming two $ characters never appear next to each other in
other circumstances:

$code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;

....but i've the lines of my source.tex file one-by-one:

while ($r = <IN> ) {
$line = ...
...
print OUT $line; }
 
L

Lars Haugseth

* gooliver said:
Assuming two $ characters never appear next to each other in
other circumstances:

$code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;

...but i've the lines of my source.tex file one-by-one:

while ($r = <IN> ) {
$line = ...
...
print OUT $line; }

Do you have to do it this way? Consider using slurp:

my $code = do { local $/; <IN> };

If you're processing one line at a time, you'll have to use a flag
indicating whether you're inside a $$-block or not, and substituting
accordingly.
 
P

Paul Lalli

gooliver said:
Assuming two $ characters never appear next to each other in
other circumstances:

$code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;

...but i've the lines of my source.tex file one-by-one:

while ($r = <IN> ) {
$line = ...
...
print OUT $line; }

"Doctor, Doctor, it hurts when I raise my arm like this!!"

Paul Lalli
 
G

gooliver

"Doctor, Doctor, it hurts when I raise my arm like this!!"

The doctor replies, "Then don't raise your arm over your head." (-:

....
$code="";
while ( $r = <IN> ) {
$code = $code . $r;
}
close(IN);
$code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
print OUT $code;
close(OUT);
 
A

anno4000

gooliver said:
Assuming two $ characters never appear next to each other in
other circumstances:

$code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;

...but i've the lines of my source.tex file one-by-one:

while ($r = <IN> ) {
$line = ...
...
print OUT $line; }

Then your subject is misleading. You don't have multiline
substitutions.

my @replace = qw( [ ]);
my $which = 0;
while ( <DATA> ) {
s/^\$\$$/$replace[ $which]/ and $which = 1 - $which;
print;
}

Anno
 
U

Uri Guttman

g> The doctor replies, "Then don't raise your arm over your head." (-:

g> ...
g> $code="";

not needed with .=.

and you didn't use strict or warnings.

g> while ( $r = <IN> ) {
g> $code = $code . $r;
g> }

gack! use File::Slurp. yours is the slowest way to read in whole files.

g> close(IN);
g> $code =~ s{ \$\$ (.*?) \$\$ }{\\[$1\\]}gxms;
g> print OUT $code;

File::Slurp can also handle outputting files.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top