Professional Web Applications Themes

Metacharacter problem in regexp - PERL Beginners

Hi! I'm making a text replace and my text to search includes a metacharacter (. The text to be searched has been stored in a scalar for example $search. The text where the search will be made has been stored for example in a scalar $text. Here is what I'm doing: $search = "for example (this)"; $text = "looking for example (this)"; $toBeReplaced = "that"; $text =~ s|$search|$toBeReplaced|; That doesn't work, if you make it this way: $text =~ s|for example \(this\)|$toBeReplaced|; works fine, but how can I backslash the metacharacters in a scalar? Petri Rautakoski...

  1. #1

    Default Metacharacter problem in regexp

    Hi!

    I'm making a text replace and my text to search includes a metacharacter (.
    The text to be searched has been stored in a scalar for example $search.
    The text where the search will be made has been stored for example in a scalar
    $text. Here is what I'm doing:

    $search = "for example (this)";
    $text = "looking for example (this)";
    $toBeReplaced = "that";

    $text =~ s|$search|$toBeReplaced|;

    That doesn't work, if you make it this way:

    $text =~ s|for example \(this\)|$toBeReplaced|;

    works fine, but how can I backslash the metacharacters in a scalar?

    Petri Rautakoski

    Petri Guest

  2. #2

    Default Re: Metacharacter problem in regexp

    Petri Rautakoski wrote:
     

    Lookup "quotemeta"

    qr//;

    -Bill-
    __Sx__________________________________________
    http://youve-reached-the.endoftheinternet.org/
    Wc Guest

  3. #3

    Default Re: Metacharacter problem in regexp

    On 03/04/04 02:19, Petri Rautakoski wrote: 

    You need to "quote" the search string:

    s/\Q$search\E/$toBeReplaced/;

    Anytime you see a variable used as a search string it should probably be
    quoted as that's an indicator that the string may come from an source
    outside of your control and thus might contain metachars.

    See Also: perlre(1)

    Randy.
    Randy Guest

  4. #4

    Default Re: Metacharacter problem in regexp

    I already tried that, but it didn't work for me, don't know why.
    The \Q$pattern\E worked so my problem is solved.

    Petri
     
    >
    > Lookup "quotemeta"
    >
    > qr//;
    >
    > -Bill-
    > __Sx__________________________________________
    > http://youve-reached-the.endoftheinternet.org/
    >[/ref]

    Petri Guest

  5. #5

    Default Re: Metacharacter problem in regexp

    Petri Rautakoski wrote: 

    Hello,
     

    If you are just searching for plain text strings then you can do this instead:

    substr $text, index( $text, $search ), length( $search ), $toBeReplaced;


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

  6. #6

    Default Re: Metacharacter problem in regexp

    Petri Rautakoski wrote: 

    $text =~ s|\Q$search\E|$toBeReplaced|;

    See: perldoc perlre

    --
    ZSDC


    Zsdc Guest

  7. #7

    Default Re: Metacharacter problem in regexp

    zsdc wrote:
     
    >
    > $text =~ s|\Q$search\E|$toBeReplaced|;
    >
    > See: perldoc perlre[/ref]

    Hello Petri,

    In Apocalypse 5 on Pattern Matching by Larry Wall, which is about what's
    wrong with regular expressions in Perl 5 and how are they going to look
    like in Perl 6, there is one part about the very problem with variables
    interpolation you've asked about and the klunky workaround I've sent
    you, which Larry himself considers to be one of the most important
    problems with Perl's regular expressions today. I thought you might be
    interested, so let me quote that part of Apocalypse 5:

    : Too hard to match a literal string
    :
    : Since regexes undergo an interpolation pass before they're
    : compiled, anything you interpolate is forced to be treated
    : as a regular expression. Often that's not what you want,
    : so we have the klunky \Q$string\E mechanism to hide regex
    : metacharacters. And that's because...
    :
    : Two-level interpretation is problematic
    :
    : The problem with \Q$string\E arises because of the
    : fundamental mistake of using interpolation to build regexes
    : instead of letting the regex control how it treats the
    : variables it references. Regexes aren't strings, they're
    : programs. Or, rather, they're strings only in the sense that
    : any piece of program is a string. Just as you have to work to
    : eval a string as a program, you should have to work to eval a
    : string as a regular expression. Most people tend to expect a
    : variable in a regular expression to match its contents literally.
    : Perl violates that expectation. And because it violates that
    : expectation, we can't make $1 synonymous with \1. And interpolated
    : parentheses throw off the capture count, so you can't easily use
    : interpolation to call subrules, so we invented (??{$var}) to get
    : around that. But then you can't actually get at the parentheses
    : captured by the subrule. The ramifications go on and on.

    From Apocalypse 5 by Larry Wall, 4 June 2002

    http://dev.perl.org/perl6/apocalypse/5

    --
    ZSDC Perl and Systems Security Consulting

    Zsdc Guest

Similar Threads

  1. Some Regexp
    By Dmitry in forum Ruby
    Replies: 19
    Last Post: December 3rd, 06:20 PM
  2. regexp splitting problem
    By Brett in forum Ruby
    Replies: 4
    Last Post: December 3rd, 01:17 AM
  3. regexp problem
    By Taylor York in forum PHP Development
    Replies: 14
    Last Post: October 17th, 03:11 PM
  4. regexp help...
    By Doug La Farge in forum PHP Development
    Replies: 1
    Last Post: July 21st, 06:47 PM
  5. regexp problem with UTF8
    By Risto Vaarandi in forum PERL Miscellaneous
    Replies: 0
    Last Post: July 16th, 03:03 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139