Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > what are included in metacharacters for regex?

Reply
Thread Tools

what are included in metacharacters for regex?

 
 
http://ejobseek.com
Guest
Posts: n/a
 
      09-01-2003
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//?


 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-01-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
 
 
 
http://ejobseek.com
Guest
Posts: n/a
 
      09-01-2003
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" <(E-Mail Removed)> дʼ
news:(E-Mail Removed)...
> 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
> (E-Mail Removed) Perl programming
> Fort Worth, Texas



 
Reply With Quote
 
Mark Jason Dominus
Guest
Posts: n/a
 
      09-01-2003
In article <bj01qc$crk$(E-Mail Removed)>,
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.
 
Reply With Quote
 
Bob Walton
Guest
Posts: n/a
 
      09-01-2003
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

 
Reply With Quote
 
Steve D
Guest
Posts: n/a
 
      09-01-2003
"http://ejobseek.com" <_@_._> wrote in message news:<bj01qc$crk$(E-Mail Removed)>...
> 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.).
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-02-2003
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
 
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
piped open and shell metacharacters John Kelly Perl Misc 22 08-03-2010 09:18 PM
function or operator to ignore metacharacters like + kelly Perl 1 01-03-2006 09:33 PM
How to remove `` \x{d} metacharacters from strings ... C. Colin Backslas Perl Misc 1 09-14-2005 10:26 AM
html form metacharacters? Dfenestr8 HTML 2 01-19-2005 12:03 PM
Avoiding shell metacharacters in os.popen Nick Craig-Wood Python 10 10-01-2004 10:02 AM



Advertisments