perl regular expression

Discussion in 'Perl Misc' started by kiranmn@my-deja.com, Apr 13, 2007.

  1. Guest

    hi,

    I want all doublequote in a string to be prefixed with '\'. I want
    to achive this with a regular expression and this regular expression
    will be in a loop, so for the same input if this same RE runs multiple
    times, it will add multiple '\` before doublequote, to avoid that i
    am checking for " NOT A BACKSLASH" in following RE.

    Following RE works for me, but it fails for two consecutive
    doublequotes.
    Why RE is failing to replace second double quote, even with /g ?

    regards,
    kiran

    ------------------------------------------
    Expect output
    input is abc "" c " de
    output is abc \"\" c \" de
    ------------------------------------------
    current output
    input is abc "" c " de
    output is abc \"" c \" de
    =========================
    $input='abc "" c " de';
    print "input is $input\n";;
    $input=~ s/([^\\x])"/$1\\"/g;
    print "output is $input\n";;
    ============================
     
    , Apr 13, 2007
    #1
    1. Advertising

  2. Mirco Wahab Guest

    wrote:
    > I want all doublequote in a string to be prefixed with '\'. I want
    > to achive this with a regular expression and this regular expression
    > will be in a loop, so for the same input if this same RE runs multiple
    > times, it will add multiple '\` before doublequote, to avoid that i
    > am checking for " NOT A BACKSLASH" in following RE.
    >
    > Following RE works for me, but it fails for two consecutive
    > doublequotes.
    > Why RE is failing to replace second double quote, even with /g ?


    You modifying the string contents of
    character positions to match in the
    next run of /g. By one \\ each time.

    So you may go back in the match position
    by one on each /g step:

    ...

    while ($input =~ s/([^\\x])"/$1\\"/g) {
    pos($input)--;
    }

    ...


    BTW. what does the 'x' in the
    character class mean?

    Regards

    M.
     
    Mirco Wahab, Apr 13, 2007
    #2
    1. Advertising

  3. Mirco Wahab Guest

    wrote:
    > I want all doublequote in a string to be prefixed with '\'. I want
    > to achive this with a regular expression and this regular expression
    > will be in a loop, so for the same input if this same RE runs multiple
    > times, it will add multiple '\` before doublequote, to avoid that i
    > am checking for " NOT A BACKSLASH" in following RE.
    >
    > Following RE works for me, but it fails for two consecutive
    > doublequotes.
    > Why RE is failing to replace second double quote, even with /g ?


    You are modifying the string contents of character positions
    for the next match by one \\ each time.

    You could go back in the match position
    by one on each match or simply match
    successively:

    ...

    1 while $input =~ s/([^\\x])"/$1\\"/g;

    ...


    BTW. what does the 'x' in the
    character class mean?

    Regards

    M.
     
    Mirco Wahab, Apr 13, 2007
    #3
  4. Mirco Wahab Guest

    Mirco Wahab wrote:
    > ...
    >
    > 1 while $input =~ s/([^\\x])"/$1\\"/g;
    >
    > ...


    Of course, if your whole problem is
    to escape quotes which aren't quoted
    already, you could do a "double magical"

    $input =~ s|(?<!\\)(?=")|\\|g;

    which combines two zero-width assertions

    (?<!\\) ==> not a \\
    (?=") ==> before a quote

    and replaces that 'cleft' by a \\

    Regards

    M.
     
    Mirco Wahab, Apr 13, 2007
    #4
  5. Dr.Ruud Guest

    schreef:

    > Following RE works for me, but it fails for two consecutive
    > doublequotes.
    > Why RE is failing to replace second double quote, even with /g ?
    >
    > $input='abc "" c " de';
    > print "input is $input\n";;
    > $input=~ s/([^\\x])"/$1\\"/g;
    > print "output is $input\n";;


    You could use

    1 while $input =~ s/([^\\x])"/$1\\"/;

    but that would go wrong for a " at the start anyway.

    This way is much clearer:

    #!/usr/bin/perl
    use strict;
    use warnings;

    my $input = q{"abc "\\" c " de};
    print qq{input is $input\n};

    for (1..4) {
    $input =~ s# (?<! \\ ) " #\\"#xg;
    print qq{output is $input\n};
    }

    --
    Affijn, Ruud

    "Gewoon is een tijger."
     
    Dr.Ruud, Apr 14, 2007
    #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. Eric B.
    Replies:
    1
    Views:
    432
    Jim Gibson
    Dec 17, 2004
  2. VSK
    Replies:
    2
    Views:
    2,305
  3. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    851
    Alan Moore
    Dec 2, 2005
  4. GIMME
    Replies:
    3
    Views:
    11,972
    vforvikash
    Dec 29, 2008
  5. Bill Chiu
    Replies:
    4
    Views:
    440
    Uwe Schnitker
    Sep 12, 2003
Loading...

Share This Page