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

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

Re: Use the characters "+" and "-" in regular expressions


From: Skip Montanaro
Subject: Re: Use the characters "+" and "-" in regular expressions
Date: Wed, 19 May 2021 11:39:46 -0500

> > Yes, use character sets, e.g. [+] and [-].

> Plus "+" is special. Minus "-" is not.
...
> Or escape with backslash. Note that due to string syntax you need
> two of them.

Yes, correct you are, Tomas. I long ago got used to putting most
punctuation in character sets regardless of its specialness, if for no
other reason than to help them stand out when rereading the code
later. I also find backslashes ugly (especially within Lisp strings
where there is no such thing as a "raw" string such as in Python, so
you wind up doubling them). Also, by reflexively putting punctuation
in character sets I rarely have to remember which are special, or in
what contexts. For example, "$" is special at the end of a regular
expression, but not elsewhere. Similarly, "^" is special only at the
start. The various special cases just make it kind of tedious and easy
to get wrong. By placing punctuation in character sets you declare to
the reader, "This is not special, no matter what you might think."
Well, except for hyphens inside character sets. :) Still, that reduces
the special cases from N to 1.

Skip



reply via email to

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