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

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

Re: Function-Problem


From: tomas
Subject: Re: Function-Problem
Date: Wed, 17 Aug 2016 20:07:27 +0200
User-agent: Mutt/1.5.21 (2010-09-15)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Wed, Aug 17, 2016 at 10:33:19AM -0700, John Mastro wrote:
> Klaus Jantzen <address@hidden> wrote:
> > (defun blank-line-p ()
> >   "Returns t if line contains only blanks or empty"
> >   (save-excursion
> >     (beginning-of-line)
> >     ; (if (looking-at-p "^[ ]+$\|^$") ; (1)
> >     ; (if (looking-at-p "^[ ]+$")   ; (2)
> >     ; (if (looking-at-p "^$")       ; (3)
> >     (if (looking-at-p "^$\|^[ ]+$") ; (1a)
> >         (progn ; (goto-char cpp) ; line is blank/empty
> >                (message "t")
> >                t)
> >         (progn ; (goto-char cpp) ; line is not blank/empty
> >                (message "nil")
> >                nil)
> >         )
> >     )
> >   ) ; end of 'blank-line-p'
> >
> > =====
> >
> > This leads to a problem with the regular expression;
> >
> > if-(3) works, if-(2) works, but the combined regular expression in
> > if-(1) or if-(1a) does not work.
> >
> > Where is my error in the RE? Is the combination not allowed in
> > 'lookig-at-p'?
> 
> This should do the trick: (looking-at-p "^[[:blank:]]*$")
> 
> The problem is that the + metacharacter means "one or more", whereas you
> want to express "zero or more" spaces. The metacharacter for that is *.

[rest elided]

I concur. The regular expression "^[ ]+$\|^$" is just a roundabout way
to spell "^[ ]*$" (i.e. say "zero or more" instead of "one or more OR zero").
And a POSIX class makes it more general (or just leave out the [] if you
really mean only the space char). But...

the expression at (1) still should work. Why it doesn't?

Well, the escape char before the \ is seen by the string reader and
disappears! To pass one escape to the regexp you have to double it.
The correct spelling for (1) would be:

  (looking-at-p "^[ ]+$\\|^$")

Note the double backslassh: one for the string syntax, one for the
regexp.

This is a very common trap. Especially because you don't need this
doubling in interactive input.

Regards
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)

iEYEARECAAYFAle0p98ACgkQBcgs9XrR2kZvLQCfZelDtIAOx/0AWiGDvKuklrQ+
GJUAmwQFnHcEmlllr+ROR9uT+EEhlG50
=ur24
-----END PGP SIGNATURE-----



reply via email to

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