[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
- [External] : Use the characters "+" and "-" in regular expressions, (continued)
- Re: [External] : Use the characters "+" and "-" in regular expressions, Jean Louis, 2021/05/21
- Re: [External] : Use the characters "+" and "-" in regular expressions, Jean Louis, 2021/05/20
- Dash in regexp character classes: Beginnig vs. end [was: Use the characters "+" and "-" in regular expressions], address@hidden, 2021/05/20
- Re: Dash in regexp character classes: Beginnig vs. end [was: Use the characters "+" and "-" in regular expressions], Robert Pluim, 2021/05/20
- RE: [External] : Re: Use the characters "+" and "-" in regular expressions, Drew Adams, 2021/05/19
- RE: [External] : Re: Use the characters "+" and "-" in regular expressions, Drew Adams, 2021/05/19
Re: Use the characters "+" and "-" in regular expressions, tomas, 2021/05/19
RE: [External] : Use the characters "+" and "-" in regular expressions, Drew Adams, 2021/05/19