Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > s/// does not affect pos?

Reply
Thread Tools

s/// does not affect pos?

 
 
Florian Kaufmann
Guest
Posts: n/a
 
      01-11-2008
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 $_;

 
Reply With Quote
 
 
 
 
Florian Kaufmann
Guest
Posts: n/a
 
      01-11-2008
the modifier for the s<><> operator in my example should of course be
s not m, in order for . to match also newlines
 
Reply With Quote
 
 
 
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      01-11-2008
On Jan 11, 4:13 am, Florian Kaufmann <(E-Mail Removed)> wrote:
> 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 $_;


Here's a different approach via composition:

my @SQ = ( qw/ \begin{YasSQ} \end{YasSQ} / );
my @DQ = ( qw/ \begin{YasDQ} \end{YasDQ} / );
my $str = "";
while( m/\G(.*?)
(["'])
(.*?)
\2
/sxg ) {

$str .= ( $2 eq "'" ? qq[$1$SQ[0]$2$3$2$SQ[1]]
: qq[$1$DQ[0]$2$3$2$DQ[1]] );
}

--
Charles DeRykus

 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      01-12-2008
Florian Kaufmann <(E-Mail Removed)> wrote:
> 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}



my %q = ( q/"/ => 'YasDQ',
q/'/ => 'YasSQ'
);

s/(['"])(.*?)\1/\\begin{$q{$1}}$1$2$1\\end{$q{$1}}/gs;


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
comp.llang.perl.moderated
Guest
Posts: n/a
 
      01-12-2008
On Jan 11, 10:15 am, "comp.llang.perl.moderated" <c...@blv-
sam-01.ca.boeing.com> wrote:
> On Jan 11, 4:13 am, Florian Kaufmann <(E-Mail Removed)> wrote:
>
>
>
> > 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}

>
> ...
>
> Here's a different approach via composition:
>
> my @SQ = ( qw/ \begin{YasSQ} \end{YasSQ} / );
> my @DQ = ( qw/ \begin{YasDQ} \end{YasDQ} / );
> my $str = "";
> while( m/\G(.*?)
> (["'])
> (.*?)
> \2
> /sxg ) {
>
> $str .= ( $2 eq "'" ? qq[$1$SQ[0]$2$3$2$SQ[1]]
> : qq[$1$DQ[0]$2$3$2$DQ[1]] );
>
> }


This 'solution' has problems... won't handle trailing characters after
the final quote-pair
for instance. See Tad's solution.

--
Charles DeRykus

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How does company culture affect you? bruce_taylor@unisoncoaching.com XML 0 09-25-2005 11:50 PM
How does company culture affect you? bruce_taylor@unisoncoaching.com Java 4 09-23-2005 03:41 AM
ACL on the DMZ does not affect VPN Users. Eddie Cisco 5 05-26-2004 07:34 AM
How does Output Cache affect an SSL (https://) connection? mmike ASP .Net 0 05-19-2004 08:34 AM
Nikon VR, how does it affect battery life? Andrew McDonald Digital Photography 4 12-04-2003 01:49 AM



Advertisments