[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Wed, 17 Aug 2016 20:07:27 +0200
-----BEGIN PGP SIGNED MESSAGE-----
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 *.
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
This is a very common trap. Especially because you don't need this
doubling in interactive input.
- -- t
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.12 (GNU/Linux)
-----END PGP SIGNATURE-----