what are included in metacharacters for regex?

  • Thread starter http://ejobseek.com
  • Start date
H

http://ejobseek.com

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//?
 
T

Tad McClellan

http://ejobseek.com said:
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/// has nothing to do with regular expressions, it is a whole
different operator.
 
H

http://ejobseek.com

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!
 
M

Mark Jason Dominus

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.
 
B

Bob Walton

http://ejobseek.com said:
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.

....
 
S

Steve D

http://ejobseek.com said:
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.).
 
A

Anno Siegel

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top