21

I need to perform regex query replace, such that

foo in foo bar is matched, but foo in foo baz is not. Normally I would use regex look ahead, e.g. foo(?=bar).

However, it seems like Emacs cannot do this? Vim seems capable, but evil mode in spacemacs cannot.

2
  • There's also this: github.com/gamesun/emacs-regex-lookaround, but I didn't try building with this patch, and it looks like it's been a while.
    – wvxvw
    Commented May 16, 2017 at 3:35
  • Shelling out to perl can work in some use cases.
    – HappyFace
    Commented Sep 19, 2021 at 7:25

2 Answers 2

20

No, Emacs regular expressions do not support arbitrary zero-width look-ahead/behind assertions.

n.b. Evil and Spacemacs (like all elisp libraries) are irrelevant when it comes to questions about the Emacs Lisp language implementation.

5
  • So is it impossible to replace foo in foobar but not in foobaz? This is a common enough operation that I thought there must be a solution. Perhaps looking ahead and behind is the wrong approach?
    – Heisenberg
    Commented May 14, 2017 at 7:36
  • 2
    This isn't strictly equivalent, but replacing foo\(bar\) with baz\1 will be sufficient in all cases where bar didn't contain further matches.
    – phils
    Commented May 14, 2017 at 8:37
  • If you're writing elisp, you have lots of flexibility, and if you're replacing interactively you can invoke arbitrary elisp during replacements, so ultimately there are ways to do these things. Just not as conveniently as you might do with those assertions.
    – phils
    Commented May 14, 2017 at 8:41
  • e.g.: replacing foo with \,(if (looking-at "bar") "baz" \&) will replace foo with baz if the following text is bar (and replace foo with foo otherwise). Which still isn't the same thing as only matching foo when it's followed by bar, but it's another option.
    – phils
    Commented May 14, 2017 at 8:48
  • I guess this is like what you said above, but it seems like you can type: C-S-M-% foo \(bar\) RET FOO \1 RET to replace all foo bar with FOO bar, but leave all foo baz alone. Is this missing some thing you wanted? Commented May 14, 2017 at 16:09
6

https://github.com/benma/visual-regexp-steroids.el/

Visual regexp steroids allows you to replace, search, etc. using python regex. Python regex has support for look ahead and look behind.

It even highlights the regexp expressions for you.

regexp example

1
  • Well, highlighting is also available without that, at least I have it in Doom, but good tip :)
    – xeruf
    Commented Jul 3, 2021 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.