what are included in metacharacters for regex?

Discussion in 'Perl Misc' started by http://ejobseek.com, Sep 1, 2003.

  1. are those included in metacharacters? anyone got a full list of them?

    ? \ "

    can anyone give some example of s/// using \Q\E and without using it which
    give two different results. i don't understand the example in perldoc which
    matches di other than die? why it does not match die?? becaues of e?? what
    is e?? matching e once or more? than still die? should bed matched...

    finial quesiton: when metacharacters are used and \Q\E must be used? left
    side of s/// how about m//? tr//?
    http://ejobseek.com, Sep 1, 2003
    #1
    1. Advertising

  2. http://ejobseek.com <_@_._> wrote:

    > are those included in metacharacters?
    >
    > ? \ "



    yes (twice), yesish and no.

    Question mark has *two* meta-meanings depending on how it is used:

    zero or one of the previous thing

    make the previous quantifier non-greedy

    Backslash is meta-ish. It is used to escape metachars, it is also
    used to introduce backslash escapes.

    Some backslash escapes apply to all double-quotish strings (eg \n),
    some only to regexes (eg \s).

    Double quotes are not meta in regexes.


    > can anyone give some example of s/// using \Q\E and without using it which
    > give two different results.


    Use \Q or quotemeta() when you have metachars that you
    want to match literally.

    Match the 5 chars in double quotes:

    $_ = 'he said "what?"';
    print "matched without \\Q\n" if /"what?"/;
    print "matched with \\Q\n" if /\Q"what?"/;

    If you like, you can just put in the backslashes yourself:

    print "matched with \\Q\n" if /\"what\?\"/;

    But \Q is over-zealous, it backslashes some chars that are NOT meta, so:

    print "matched with \\Q\n" if /"what\?"/;

    would be good enough to get the intended result.


    > i don't understand the example in perldoc which



    Which example "in perldoc"?


    > matches di other than die? why it does not match die??



    I'm not going to spend 10 minutes trying to find what you
    are talking about, so I can't help with that part of
    your question.


    > what
    > is e??



    Just what perlop.pod says it is for s/// :

    e Evaluate the right side as an expression.


    > finial quesiton: when metacharacters are used and \Q\E must be used?



    It depends on whether you _want_ the meta characters to have their
    meta-meaning, or whether you _want_ to match the literal characters.


    > left
    > side of s/// how about m//?



    Yes, in the PATTERN part for both operators.


    > tr//?



    tr/// has nothing to do with regular expressions, it is a whole
    different operator.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 1, 2003
    #2
    1. Advertising

  3. the example in perlfaq is:

    How can I quote a variable to use in a regex?
    The Perl parser will expand $variable and @variable references
    in regular expressions unless the delimiter is a single quote.
    Remember, too, that the right-hand side of a "s///" substitution
    is considered a double-quoted string (see perlop for more
    details). Remember also that any regex special characters will
    be acted on unless you precede the substitution with \Q. Here's
    an example:

    $string = "to die?";
    $lhs = "die?";
    $rhs = "sleep, no more";

    $string =~ s/\Q$lhs/$rhs/;
    # $string is now "to sleep no more"

    Without the \Q, the regex would also spuriously match "di".


    I DON'T UNDERSTAND WHY DI IS MATCHED!

    "Tad McClellan" <> дÈëÓʼþ
    news:...
    > http://ejobseek.com <_@_._> wrote:
    >
    > > are those included in metacharacters?
    > >
    > > ? \ "

    >
    >
    > yes (twice), yesish and no.
    >
    > Question mark has *two* meta-meanings depending on how it is used:
    >
    > zero or one of the previous thing
    >
    > make the previous quantifier non-greedy
    >
    > Backslash is meta-ish. It is used to escape metachars, it is also
    > used to introduce backslash escapes.
    >
    > Some backslash escapes apply to all double-quotish strings (eg \n),
    > some only to regexes (eg \s).
    >
    > Double quotes are not meta in regexes.
    >
    >
    > > can anyone give some example of s/// using \Q\E and without using it

    which
    > > give two different results.

    >
    > Use \Q or quotemeta() when you have metachars that you
    > want to match literally.
    >
    > Match the 5 chars in double quotes:
    >
    > $_ = 'he said "what?"';
    > print "matched without \\Q\n" if /"what?"/;
    > print "matched with \\Q\n" if /\Q"what?"/;
    >
    > If you like, you can just put in the backslashes yourself:
    >
    > print "matched with \\Q\n" if /\"what\?\"/;
    >
    > But \Q is over-zealous, it backslashes some chars that are NOT meta, so:
    >
    > print "matched with \\Q\n" if /"what\?"/;
    >
    > would be good enough to get the intended result.
    >
    >
    > > i don't understand the example in perldoc which

    >
    >
    > Which example "in perldoc"?
    >
    >
    > > matches di other than die? why it does not match die??

    >
    >
    > I'm not going to spend 10 minutes trying to find what you
    > are talking about, so I can't help with that part of
    > your question.
    >
    >
    > > what
    > > is e??

    >
    >
    > Just what perlop.pod says it is for s/// :
    >
    > e Evaluate the right side as an expression.
    >
    >
    > > finial quesiton: when metacharacters are used and \Q\E must be used?

    >
    >
    > It depends on whether you _want_ the meta characters to have their
    > meta-meaning, or whether you _want_ to match the literal characters.
    >
    >
    > > left
    > > side of s/// how about m//?

    >
    >
    > Yes, in the PATTERN part for both operators.
    >
    >
    > > tr//?

    >
    >
    > tr/// has nothing to do with regular expressions, it is a whole
    > different operator.
    >
    >
    > --
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
    http://ejobseek.com, Sep 1, 2003
    #3
  4. In article <bj01qc$crk$>,
    http://ejobseek.com <_@_._> wrote:
    >the example in perlfaq is:
    >
    > $string = "to die?";
    > $lhs = "die?";
    > $rhs = "sleep, no more";
    >
    > $string =~ s/\Q$lhs/$rhs/;
    > # $string is now "to sleep no more"
    >
    > Without the \Q, the regex would also spuriously match "di".
    >
    >
    >I DON'T UNDERSTAND WHY DI IS MATCHED!


    It isn't. It is a bad example. Here is a better example:

    $regex = 'die?';
    $target = "diamonds";
    $target =~ s/$regex/-----/;
    print $target;

    prints

    -----amonds


    This is because in a regex, '?' means that the previous item is
    optional. The regex /die?/ matches either 'di' or 'die', because 'e?'
    means that the 'e' is optional. Since 'diamonds' contains 'di', the
    /die?/ pattern matches the 'di' part of 'diamonds'.

    Using \Q tells Perl that none of the special characters (such as '?')
    in a regex should be treated specially. With \Q, the 'e>' no longer
    means an optional 'e'; it means an 'e' character followed by a '?'
    character. The same example, with \Q:

    $regex = 'die?';
    $target = "diamonds";
    $target =~ s/\Q$regex/-----/;
    print $target;

    prints

    diamonds

    because the regex did not match, because $target did not contain "die?".

    I hope this helps.
    Mark Jason Dominus, Sep 1, 2003
    #4
  5. http://ejobseek.com

    Bob Walton Guest

    http://ejobseek.com wrote:

    > the example in perlfaq is:
    >
    > How can I quote a variable to use in a regex?
    > The Perl parser will expand $variable and @variable references
    > in regular expressions unless the delimiter is a single quote.
    > Remember, too, that the right-hand side of a "s///" substitution
    > is considered a double-quoted string (see perlop for more
    > details). Remember also that any regex special characters will
    > be acted on unless you precede the substitution with \Q. Here's
    > an example:
    >
    > $string = "to die?";
    > $lhs = "die?";
    > $rhs = "sleep, no more";
    >
    > $string =~ s/\Q$lhs/$rhs/;
    > # $string is now "to sleep no more"
    >
    > Without the \Q, the regex would also spuriously match "di".
    >
    >
    > I DON'T UNDERSTAND WHY DI IS MATCHED!



    DI won't be matched. di will be because:

    $string =~ s/$lhs/$rhs/;

    would have its regexp interpolated to form:

    $string =~ s/die?/$rhs/;

    If $string contained, for example:

    $string = "to di?"

    then

    $string =~ s/die?/$rhs/;

    would match. It matches because the regexp /die?/ means a "d" followed
    by an "i" followed by zero or one "e"'s, which is a perfect description
    of di .

    When \Q is used as in

    $string =~ s/\Q$lhs/$rhs/;

    then the quotemeta() function is applied to the interpolated soon-to-be
    regexp, forming:

    $string =~ s/die\?/$rhs/;

    In that case the ? is literal (because it is escaped by a \), and is not
    interpreted as a metacharacter, so the regexp won't match the string "to
    di?" (because there is no "e" character in the string as required by the
    regexp).

    HTH.


    >

    ....

    --
    Bob Walton
    Bob Walton, Sep 1, 2003
    #5
  6. http://ejobseek.com

    Steve D Guest

    "http://ejobseek.com" <_@_._> wrote in message news:<bj01qc$crk$>...
    > Without the \Q, the regex would also spuriously match "di".
    > I DON'T UNDERSTAND WHY DI IS MATCHED!


    Because "e?" matches an "e" or "" (nothing) when "?" is an interpreted
    character. In addition, without the \Q, the code would change
    'to die?'
    into
    'to sleep, no more?'
    (the "?" would not be eliminated.).
    Steve D, Sep 1, 2003
    #6
  7. http://ejobseek.com

    Anno Siegel Guest

    http://ejobseek.com <_@_._> wrote in comp.lang.perl.misc:

    Don't top-post.

    > the example in perlfaq is:
    >
    > How can I quote a variable to use in a regex?
    > The Perl parser will expand $variable and @variable references
    > in regular expressions unless the delimiter is a single quote.
    > Remember, too, that the right-hand side of a "s///" substitution
    > is considered a double-quoted string (see perlop for more
    > details). Remember also that any regex special characters will
    > be acted on unless you precede the substitution with \Q. Here's
    > an example:
    >
    > $string = "to die?";
    > $lhs = "die?";
    > $rhs = "sleep, no more";
    >
    > $string =~ s/\Q$lhs/$rhs/;
    > # $string is now "to sleep no more"
    >
    > Without the \Q, the regex would also spuriously match "di".
    >
    >
    > I DON'T UNDERSTAND WHY DI IS MATCHED!


    Yelling doesn't help understanding, analysis does.

    It doesn't match "di". The error is that /die?/ matches "die" without
    matching the final "?". It can't match "?" because there is no "?" in
    the regex. The "?" makes the final "e" in "die" optional (but it is
    matched anyhow).

    [tofu snipped]

    Anno
    Anno Siegel, Sep 2, 2003
    #7
    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. kelly
    Replies:
    1
    Views:
    2,358
    Jim Gibson
    Jan 3, 2006
  2. Dfenestr8

    html form metacharacters?

    Dfenestr8, Jan 19, 2005, in forum: HTML
    Replies:
    2
    Views:
    521
    Jukka K. Korpela
    Jan 19, 2005
  3. Nick Craig-Wood

    Avoiding shell metacharacters in os.popen

    Nick Craig-Wood, Sep 29, 2004, in forum: Python
    Replies:
    10
    Views:
    596
    David Fraser
    Oct 1, 2004
  4. C. Colin Backslas
    Replies:
    1
    Views:
    226
    Anno Siegel
    Sep 14, 2005
  5. John Kelly

    piped open and shell metacharacters

    John Kelly, Jul 30, 2010, in forum: Perl Misc
    Replies:
    22
    Views:
    383
    Ilya Zakharevich
    Aug 3, 2010
Loading...

Share This Page