12

Why does replace-regexp backwards work so differently?

C-u - M-x replace-regexp \w+

The - prefix arg replaces backwards but it hits one char at a time, as if the plus sign weren't there. The same replacement forwards (without the prefix arg) does hit one word at a time. What's going on, @emacs@lemmy.ml?

top 6 comments
sorted by: hot top controversial new old
[-] 0v0@sopuli.xyz 1 points 1 year ago

Because once it hits the ultimate character of a word, \w+ matches that (single) character, next time it matches the penultimate character, etc. You'd need \W\w+ to make it look far enough back to the beginning of the word.

[-] Sandra@idiomdrottning.org 0 points 1 year ago

So it's literally looking at the regexp backwards and forwards at the same time ๐Ÿ˜ตโ€๐Ÿ’ซ

@0v0

[-] 0v0@sopuli.xyz 1 points 1 year ago

The regexp itself always looks forward, the BACKWARD argument just determines which direction the point should move after a match.

[-] Sandra@idiomdrottning.org 0 points 1 year ago* (last edited 1 year ago)

But what's so weird is that replace-regexp forwards with \w+ replaces all words, while replace-regexp backwards with \w+ replaces the characters individually.

@0v0 @emacs

[-] 0v0@sopuli.xyz 1 points 1 year ago

Consider the string abc. From the end, moving backwards, when does it match \w+, and what does it match? When it reaches c, it matches c. And from the front, moving forwards? When it reaches a, it matches abc. This is why it acts differently.

[-] Sandra@idiomdrottning.org 3 points 1 year ago

Yes, I got that, that wasn't the weird part. The weird part is why the matcher is searching char-by-char backwards in the first place as opposed to skipping match-by-match.

I'll use "\b\w+", that seems to work well. \W\w+ was not good since it caught the spaces.

(Thanks for your patient repeated replies, BTW, I don't mean to come across as ungrateful.)

@0v0 @emacs

this post was submitted on 11 Nov 2023
12 points (100.0% liked)

Emacs

2190 readers
1 users here now

Our infinitely powerful editor.

founded 4 years ago
MODERATORS