From: Stefan Monnier via Users list for the GNU Emacs text editor <help-gnu-emacs@gnu.org>
To: help-gnu-emacs@gnu.org
Subject: Re: Regular expressions and user-escaped characters
Date: Tue, 03 Dec 2024 09:01:01 -0500 [thread overview]
Message-ID: <jwv1pyog9ek.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: 87plm9g2rm.fsf@librehacker.com
> Hi, what do you do in a regular expression if you want to match a character,
> but not a the same character that has been escaped by the user. E.g., if
> I want my regular expression to look for ?\[ (ASCII 91), matching string "["
> and "a[a" but not string "\\[" or "a\\[a", if you follow me. Is this
> possible with just a regular expression?
The "usual" way we do that is with the godawful:
"\\(?:^\\|[^\\]\\(?:\\\\\\\\\\)*\\)\\["
This is careful to match the [ if it's preceded by an even number
of backslashes. But beware that it makes more than the actual [, so if
you start the search from a point that's looking at a [, it won't find
it (except if it's at the beginning of the line).
> If not, what is a good workaround?
Just use a regexp which matches all [ (regardless of any previous
backslashes) and then check afterwards, in ELisp, whether it's preceded
by an odd number of backslashes, e.g. with something like
(save-excursion
(goto-char <FOO>)
(zerop (% (skip-chars-backward "\\") 2)))
- Stefan
prev parent reply other threads:[~2024-12-03 14:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-12-02 22:04 Regular expressions and user-escaped characters Christopher Howard
2024-12-02 22:32 ` Joost Kremers
2024-12-02 22:50 ` Joost Kremers
2024-12-02 23:09 ` Joost Kremers
2024-12-03 14:01 ` Stefan Monnier via Users list for the GNU Emacs text editor [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=jwv1pyog9ek.fsf-monnier+emacs@gnu.org \
--to=help-gnu-emacs@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).