ELisp: skip-chars-forward vs re-search-forward

By Xah Lee. Date: .

skip-chars-forward vs re-search-forward

Suppose you have a URL. Cursor is somewhere on the URL, or at the beginning of the URL or at the end of the URL. You want to find the beginning position and end position of the URL.

If you use re-search-backward for whitespace, you have to consider if the URL is at the beginning of file with no character before it. With skip-chars-backward, you don't have to do this extra check.

;; compare skip-chars-forward vs re-search-forward

(save-excursion
  (skip-chars-backward "^[:space:]")
  (setq xp1 (point))
  (skip-chars-forward "^[:space:]")
  (setq xp2 (point)))

(save-excursion
  (re-search-backward "[[:space:]]" nil 1)
  (setq xp1 (1- (point)))
  (re-search-forward "[[:space:]]" nil 1)
  (setq xp2 (1- (point))))