Function: regexp-opt-depth

regexp-opt-depth is an autoloaded and byte-compiled function defined in regexp-opt.el.gz.

Signature

(regexp-opt-depth REGEXP)

Documentation

Return the depth of REGEXP.

This means the number of non-shy regexp grouping constructs
(parenthesized expressions) in REGEXP.

Other relevant functions are documented in the regexp group.

View in manual

Shortdoc

;; regexp
(regexp-opt-depth "\\(a\\(b\\)\\)")
    => 2

Aliases

c-regexp-opt-depth (obsolete since 29.1)

Source Code

;; Defined in /usr/src/emacs/lisp/emacs-lisp/regexp-opt.el.gz
;;;###autoload
(defun regexp-opt-depth (regexp)
  "Return the depth of REGEXP.
This means the number of non-shy regexp grouping constructs
\(parenthesized expressions) in REGEXP."
  (declare (pure t) (side-effect-free t))
  (save-match-data
    ;; Hack to signal an error if REGEXP does not have balanced parentheses.
    (string-match regexp "")
    ;; Count the number of open parentheses in REGEXP.
    (let ((count 0) start last)
      (while (string-match "\\\\(\\(\\?[0-9]*:\\)?" regexp start)
	(setq start (match-end 0))	      ; Start of next search.
	(when (and (not (match-beginning 1))
		   (subregexp-context-p regexp (match-beginning 0) last))
	  ;; It's not a shy group and it's not inside brackets or after
	  ;; a backslash: it's really a group-open marker.
	  (setq last start)	    ; Speed up next regexp-opt-re-context-p.
	  (setq count (1+ count))))
      count)))