Skip to main content
edited tags
Link
Drew
  • 79.1k
  • 10
  • 123
  • 257
added 535 characters in body
Source Link
ironsand
  • 423
  • 3
  • 14

How to match only a word when using `research`re-search-backward` function

I want to replace a character inside a word, so I wrote:

(setq regexp "[ab]")
(and (re-search-backward regexp nil t 1)
     (replace-match "c"))

This replaces a or b to c as I wish, but I want to set a bound of it. There is a BOUND option with re-search-backward. How can I make the match only a word that is under the current cursor?

Thanks for @jackson I can write the function, but still it doesn't work properly.

(setq regexp ".*[ab].*")
(defun test-it ()
  (interactive)
  (save-excursion
    (save-restriction
      (narrow-to-region (progn (backward-word) (point))
                        (progn (forward-word) (point)))
      (and (re-search-backward regexp)
           (replace-match "c")))))

Thins function replaces ab to ac and ad to cd as I expected, but it doesn't replace ac to cc. Why?

How to match only a word when using `research-backward` function

I want to replace a character inside a word, so I wrote:

(setq regexp "[ab]")
(and (re-search-backward regexp nil t 1)
     (replace-match "c"))

This replaces a or b to c as I wish, but I want to set a bound of it. There is a BOUND option with re-search-backward. How can I make the match only a word that is under the current cursor?

How to match only a word when using `re-search-backward` function

I want to replace a character inside a word, so I wrote:

(setq regexp "[ab]")
(and (re-search-backward regexp nil t 1)
     (replace-match "c"))

This replaces a or b to c as I wish, but I want to set a bound of it. There is a BOUND option with re-search-backward. How can I make the match only a word that is under the current cursor?

Thanks for @jackson I can write the function, but still it doesn't work properly.

(setq regexp ".*[ab].*")
(defun test-it ()
  (interactive)
  (save-excursion
    (save-restriction
      (narrow-to-region (progn (backward-word) (point))
                        (progn (forward-word) (point)))
      (and (re-search-backward regexp)
           (replace-match "c")))))

Thins function replaces ab to ac and ad to cd as I expected, but it doesn't replace ac to cc. Why?

Source Link
ironsand
  • 423
  • 3
  • 14

How to match only a word when using `research-backward` function

I want to replace a character inside a word, so I wrote:

(setq regexp "[ab]")
(and (re-search-backward regexp nil t 1)
     (replace-match "c"))

This replaces a or b to c as I wish, but I want to set a bound of it. There is a BOUND option with re-search-backward. How can I make the match only a word that is under the current cursor?