emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unquoted special characters in regexps


From: Luc Teirlinck
Subject: Re: Unquoted special characters in regexps
Date: Sun, 26 Feb 2006 10:41:47 -0600 (CST)

Andreas Schwab wrote:

   > According to the Elisp manual all these exhibit "poor practice" since
   > you didn't quote the second `]'s.

   It's a bug in the manual.

I propose the following patch to lispref/searching.texi, which I can
install if desired.  I will wait till more people, in particular
Richard, have had an opportunity to see it.

Note that the current version already clearly states elsewhere that
`]' is special _inside_ character alternatives:

     Note that the usual regexp special characters are not special
     inside a character alternative.  A completely different set of
     characters is special inside character alternatives: `]', `-' and
     `^'.

Apart from correcting the bug we are discussing, it also corrects
another misstatement:

    For example, a string with unbalanced square brackets is invalid
    (with a few exceptions, such as `[]]'),

That is incorrect as the examples below show.

ELISP> (string-match "]]]]" "]]]]")
0
ELISP> (string-match "[[]" "[")
0

One correct way to restate it would be that a string whose square
brackets _with special meaning in the context in which they are used _
do not balance is invalid.  This would be (unless I overlook
something) without exceptions: in `[]]' the square brackets with
special meaning do balance.  In the patch below I formulated it
differently.

None of my previous mails to emacs-{devel,pretest-bug} in the last few
days have appeared on the list, so I wonder whether this one will.

===File ~/searching.texi-diff===============================
*** searching.texi      06 Feb 2006 16:02:08 -0600      1.68
--- searching.texi      26 Feb 2006 10:25:06 -0600      
***************
*** 237,243 ****
  special constructs and the rest are @dfn{ordinary}.  An ordinary
  character is a simple regular expression that matches that character and
  nothing else.  The special characters are @samp{.}, @samp{*}, @samp{+},
! @samp{?}, @samp{[}, @samp{]}, @samp{^}, @samp{$}, and @samp{\}; no new
  special characters will be defined in the future.  Any other character
  appearing in a regular expression is ordinary, unless a @samp{\}
  precedes it.
--- 237,243 ----
  special constructs and the rest are @dfn{ordinary}.  An ordinary
  character is a simple regular expression that matches that character and
  nothing else.  The special characters are @samp{.}, @samp{*}, @samp{+},
! @samp{?}, @samp{[}, @samp{^}, @samp{$}, and @samp{\}; no new
  special characters will be defined in the future.  Any other character
  appearing in a regular expression is ordinary, unless a @samp{\}
  precedes it.
***************
*** 740,747 ****
  
  @kindex invalid-regexp
    Not every string is a valid regular expression.  For example, a string
! with unbalanced square brackets is invalid (with a few exceptions, such
! as @samp{[]]}), and so is a string that ends with a single @samp{\}.  If
  an invalid regular expression is passed to any of the search functions,
  an @code{invalid-regexp} error is signaled.
  
--- 740,747 ----
  
  @kindex invalid-regexp
    Not every string is a valid regular expression.  For example, a string
! that ends inside a character alternative without terminating @samp{]}
! is invalid, and so is a string that ends with a single @samp{\}.  If
  an invalid regular expression is passed to any of the search functions,
  an @code{invalid-regexp} error is signaled.
  
============================================================




reply via email to

[Prev in Thread] Current Thread [Next in Thread]