help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: re-search beginning of line or whitespace


From: Neon Absentius
Subject: Re: re-search beginning of line or whitespace
Date: Thu, 3 Nov 2005 20:33:16 +0000
User-agent: Mutt/1.4.2.1i

On Wed, Nov 02, 2005 at 08:19:14AM -0900, thus spake Tim Johnson:
>   Let me see if I understand the logic of this strange creature called
>   "regular expressions":
> 
>    "^"      ;; match beginning of line
>    "\\|"    ;; seperator for alternatives
>    "[\t ]"  ;; match any of: TAB, SPACE
> 

It should really be two slashes: [\\t ].  That escaped me ;).

>    Am I correct? 
>    I get confused by the way that emacs "escapes" metacharacters.

It is explained nicely in the elisp manual:

,----[ (info "(elisp) Regexp Special") ]
| `\'
|      has two functions: it quotes the special characters (including
|      `\'), and it introduces additional special constructs.
| 
|      Because `\' quotes special characters, `\$' is a regular
|      expression that matches only `$', and `\[' is a regular expression
|      that matches only `[', and so on.
| 
|      Note that `\' also has special meaning in the read syntax of Lisp
|      strings (*note String Type::), and must be quoted with `\'.  For
|      example, the regular expression that matches the `\' character is
|      `\\'.  To write a Lisp string that contains the characters `\\',
|      Lisp syntax requires you to quote each `\' with another `\'.
|      Therefore, the read syntax for a regular expression matching `\'
|      is `"\\\\"'.
`----

You see, for the regular expression parser of emacs certain
characters have to be escaped for example "|" when one uses it to
indicate disjunction, so you write it "\|".  However when you write
a Lisp program the regexp is entered as a string, and since "\" is
an elisp escape character a sting that contains "\|" is interpreted
as "|".  You don't want that, you want the string to pass to the
"regexp machine" as is, you want the regexp to have "\|"; therefore
you have to escape the slash thus "\\|".  

I hope this makes some sense.

> 
>    Thanks again
>    tim
> 
> -- 
> Tim Johnson <tim@johnsons-web.com>
>       http://www.alaska-internet-solutions.com
> 
> 
> _______________________________________________
> Help-gnu-emacs mailing list
> Help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

-- 
Computer science is not about computers, any more than astronomy is
about telescopes.
        -- Edsger Dijkstra




reply via email to

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