From: Joost Kremers <joostkremers@fastmail.fm>
To: Christopher Howard <christopher@librehacker.com>
Cc: Help Gnu Emacs Mailing List <help-gnu-emacs@gnu.org>
Subject: Re: Regular expressions and user-escaped characters
Date: Mon, 02 Dec 2024 23:32:46 +0100 [thread overview]
Message-ID: <864j3lyaup.fsf@fastmail.fm> (raw)
In-Reply-To: <87plm9g2rm.fsf@librehacker.com> (Christopher Howard's message of "Mon, 02 Dec 2024 13:04:45 -0900")
On Mon, Dec 02 2024, Christopher Howard wrote:
> 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?
You may get away with something like "[^\\][[]", though keep in mind that
that does not match a ?[ not preceded by a backslash, but rather a ?[
preceded by a character that is not a backslash. Depending on your use
case, that might suffice, though, esp. if you use a capturing group:
```
(let ((str "a[a"))
(when (string-match "[^\\]\\([[]\\)" str)
(match-string 1 str)))
=> "["
```
vs.:
```
(let ((str "a\\[a"))
(when (string-match "[^\\]\\([[]\\)" str)
(match-string 1 str)))
=> nil
```
The "proper" way to do this would be to use negative lookbehind,
`"(?<!\\)[[])"`, but Emacs' regexp engine does not support that.
> If not, what is a good workaround? I was wondering about, say, replacing
> all the escaped characters first with some uncommon character (like a
> control code) and then converting back afterwards. But then I suppose I
> would need to do a check for that uncommon character first.
That would probably work.
--
Joost Kremers
Life has its moments
next prev parent reply other threads:[~2024-12-02 22:32 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 [this message]
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
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=864j3lyaup.fsf@fastmail.fm \
--to=joostkremers@fastmail.fm \
--cc=christopher@librehacker.com \
--cc=help-gnu-emacs@gnu.org \
/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).