s/// does not affect pos?

Discussion in 'Perl Misc' started by Florian Kaufmann, Jan 11, 2008.

  1. 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 $_;
    Florian Kaufmann, Jan 11, 2008
    #1
    1. Advertising

  2. the modifier for the s<><> operator in my example should of course be
    s not m, in order for . to match also newlines
    Florian Kaufmann, Jan 11, 2008
    #2
    1. Advertising

  3. On Jan 11, 4:13 am, Florian Kaufmann <> 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
    comp.llang.perl.moderated, Jan 11, 2008
    #3
  4. Florian Kaufmann <> 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/"
    Tad J McClellan, Jan 12, 2008
    #4
  5. 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 <> 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
    comp.llang.perl.moderated, Jan 12, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. mmike
    Replies:
    0
    Views:
    535
    mmike
    May 19, 2004
  2. Replies:
    4
    Views:
    340
  3. Erik Hollensbe
    Replies:
    0
    Views:
    91
    Erik Hollensbe
    Apr 8, 2006
  4. ReGenesis0
    Replies:
    3
    Views:
    372
    ReGenesis0
    Sep 28, 2008
  5. prati
    Replies:
    0
    Views:
    421
    prati
    Oct 27, 2012
Loading...

Share This Page