[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Regular expressions and user-escaped characters
From: |
Joost Kremers |
Subject: |
Re: Regular expressions and user-escaped characters |
Date: |
Mon, 02 Dec 2024 23:32:46 +0100 |
User-agent: |
mu4e 1.12.7; emacs 29.4 |
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