>>>>> "PN" == Pierre Nugues <pierre.nug...@cs.lth.se> writes:

  PN> I am wondering on some metacharacters in regular expressions.
  PN> According to Wall et al. (2000, p. 158), there are 12 metacharacters:
  PN> * + ? . \ | ( ) [ { ^ $
  PN> On the other hand, the Perl regular expressions quick start
  PN> (http://perldoc.perl.org/perlrequick.html ) cites 14 metacharacters:
  PN> {}[]()^$.|*+?\

  PN> First, I am not quite sure about the status of } and ]. Can somebody
  PN> explain the discrepancy between the two sources?

they are really the same lists. ) and } by themselves won't be meta as
they are only meaningful when there was a paired opening ( or {
earlier. so you could use them without escaping if there was no opening
paired char. but they are meta when used as a closing char of a pair. so
you can look at it from both views and be correct.

  PN> In addition, { and } seem to be fully acceptable characters outside an
  PN> expression like {MIN, MAX} and patterns m/{my string}/ or m/{my
  PN> string/ do not raise any exception or error. This is not the case for
  PN> ( and the instruction m/(my string/ stops the program with an
  PN> "unmatched" message. My question is: What are the different escape
  PN> treatments for the different metacharacters?

{} can only surround a quantifier value ({n}, {n,m}, {n,}) so if the
regex parser doesn't see a full quantifier it takes the string just as
normal text. on the other hand () can surround any regex and so perl
can't tell if you meant to close the open ( or not. so it decides to
assume a missing close ) and error out. the likelihood that a missing
close ) is an error is high but not for a missing close }.

  PN> A simple solution for a programmer that would like to ignore this
  PN> would be to quote every purported metacharacter, when it appears as a
  PN> literal; but I would like to understand how the algorithm really
  PN> works... Do you know where I could find a clarification on that?

read up on quotemeta which does that and is used by the \Q op in regexes.

uri

-- 
Uri Guttman  ------  u...@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to