1 ;;; rx.el --- sexp notation for regular expressions
3 ;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: Gerd Moellmann <gerd@gnu.org>
8 ;; Keywords: strings, regexps, extensions
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; This is another implementation of sexp-form regular expressions.
30 ;; It was unfortunately written without being aware of the Sregex
31 ;; package coming with Emacs, but as things stand, Rx completely
32 ;; covers all regexp features, which Sregex doesn't, doesn't suffer
33 ;; from the bugs mentioned in the commentary section of Sregex, and
34 ;; uses a nicer syntax (IMHO, of course :-).
36 ;; This significantly extended version of the original, is almost
37 ;; compatible with Sregex. The only incompatibility I (fx) know of is
38 ;; that the `repeat' form can't have multiple regexp args.
40 ;; Now alternative forms are provided for a degree of compatibility
41 ;; with Shivers' attempted definitive SRE notation
42 ;; <URL:http://www.ai.mit.edu/~/shivers/sre.txt>. SRE forms not
43 ;; catered for include: dsm, uncase, w/case, w/nocase, ,@<exp>,
44 ;; ,<exp>, (word ...), word+, posix-string, and character class forms.
45 ;; Some forms are inconsistent with SRE, either for historical reasons
46 ;; or because of the implementation -- simple translation into Emacs
47 ;; regexp strings. These include: any, word. Also, case-sensitivity
48 ;; and greediness are controlled by variables external to the regexp,
49 ;; and you need to feed the forms to the `posix-' functions to get
50 ;; SRE's POSIX semantics. There are probably more difficulties.
52 ;; Rx translates a sexp notation for regular expressions into the
53 ;; usual string notation. The translation can be done at compile-time
54 ;; by using the `rx' macro. It can be done at run-time by calling
55 ;; function `rx-to-string'. See the documentation of `rx' for a
56 ;; complete description of the sexp notation.
58 ;; Some examples of string regexps and their sexp counterparts:
61 ;; (rx (and line-start (0+ (in "a-z"))))
64 ;; (rx (and "\n" (not blank))), or
65 ;; (rx (and "\n" (not (any " \t"))))
67 ;; "\\*\\*\\* EOOH \\*\\*\\*\n"
68 ;; (rx "*** EOOH ***\n")
70 ;; "\\<\\(catch\\|finally\\)\\>[^_]"
71 ;; (rx (and word-start (submatch (or "catch" "finally")) word-end
74 ;; "[ \t\n]*:\\([^:]+\\|$\\)"
75 ;; (rx (and (zero-or-more (in " \t\n")) ":"
76 ;; (submatch (or line-end (one-or-more (not (any ?:)))))))
78 ;; "^content-transfer-encoding:\\(\n?[\t ]\\)*quoted-printable\\(\n?[\t ]\\)*"
79 ;; (rx (and line-start
80 ;; "content-transfer-encoding:"
83 ;; (+ (? ?\n)) blank))
85 ;; (concat "^\\(?:" something-else "\\)")
86 ;; (rx (and line-start (eval something-else))), statically or
87 ;; (rx-to-string '(and line-start ,something-else)), dynamically.
89 ;; (regexp-opt '(STRING1 STRING2 ...))
90 ;; (rx (or STRING1 STRING2 ...)), or in other words, `or' automatically
91 ;; calls `regexp-opt' as needed.
94 ;; (rx (or (and line-start ";;" (0+ space) ?\n)
95 ;; (and line-start ?\n)))
97 ;; "\\$[I]d: [^ ]+ \\([^ ]+\\) "
99 ;; (1+ (not (in " ")))
101 ;; (submatch (1+ (not (in " "))))
105 ;; (rx (and ?\\ ?\\ ?\[ (1+ word)))
114 (defconst rx-constituents
115 '((and .
(rx-and 1 nil
))
118 (sequence . and
) ; sregex
122 (nonl . not-newline
) ; SRE
123 (anything .
".\\|\n")
124 (any .
(rx-any 1 nil rx-check-any
)) ; inconsistent with SRE
126 (char . any
) ; sregex
127 (not-char .
(rx-not-char 1 nil rx-check-any
)) ; sregex
128 (not .
(rx-not 1 1 rx-check-not
))
129 ;; Partially consistent with sregex, whose `repeat' is like our
130 ;; `**'. (`repeat' with optional max arg and multiple sexp forms
132 (repeat .
(rx-repeat 2 3))
133 (= .
(rx-= 2 nil
)) ; SRE
134 (>= .
(rx->= 2 nil
)) ; SRE
135 (** .
(rx-** 2 nil
)) ; SRE
136 (submatch .
(rx-submatch 1 nil
)) ; SRE
138 (zero-or-more .
(rx-kleene 1 nil
))
139 (one-or-more .
(rx-kleene 1 nil
))
140 (zero-or-one .
(rx-kleene 1 nil
))
141 (\? . zero-or-one
) ; SRE
143 (* . zero-or-more
) ; SRE
146 (+ . one-or-more
) ; SRE
149 (optional . zero-or-one
)
150 (opt . zero-or-one
) ; sregex
151 (minimal-match .
(rx-greedy 1 1))
152 (maximal-match .
(rx-greedy 1 1))
153 (backref .
(rx-backref 1 1 rx-check-backref
))
155 (bol . line-start
) ; SRE
157 (eol . line-end
) ; SRE
158 (string-start .
"\\`")
159 (bos . string-start
) ; SRE
160 (bot . string-start
) ; sregex
162 (eos . string-end
) ; SRE
163 (eot . string-end
) ; sregex
164 (buffer-start .
"\\`")
168 (bow . word-start
) ; SRE
170 (eow . word-end
) ; SRE
171 (word-boundary .
"\\b")
172 (not-word-boundary .
"\\B") ; sregex
173 (symbol-start .
"\\_<")
174 (symbol-end .
"\\_>")
175 (syntax .
(rx-syntax 1 1))
176 (not-syntax .
(rx-not-syntax 1 1)) ; sregex
177 (category .
(rx-category 1 1 rx-check-category
))
178 (eval .
(rx-eval 1 1))
179 (regexp .
(rx-regexp 1 1 stringp
))
180 (digit .
"[[:digit:]]")
181 (numeric . digit
) ; SRE
183 (control .
"[[:cntrl:]]") ; SRE
184 (cntrl . control
) ; SRE
185 (hex-digit .
"[[:xdigit:]]") ; SRE
186 (hex . hex-digit
) ; SRE
187 (xdigit . hex-digit
) ; SRE
188 (blank .
"[[:blank:]]") ; SRE
189 (graphic .
"[[:graph:]]") ; SRE
190 (graph . graphic
) ; SRE
191 (printing .
"[[:print:]]") ; SRE
192 (print . printing
) ; SRE
193 (alphanumeric .
"[[:alnum:]]") ; SRE
194 (alnum . alphanumeric
) ; SRE
195 (letter .
"[[:alpha:]]")
196 (alphabetic . letter
) ; SRE
197 (alpha . letter
) ; SRE
198 (ascii .
"[[:ascii:]]") ; SRE
199 (nonascii .
"[[:nonascii:]]")
200 (lower .
"[[:lower:]]") ; SRE
201 (lower-case . lower
) ; SRE
202 (punctuation .
"[[:punct:]]") ; SRE
203 (punct . punctuation
) ; SRE
204 (space .
"[[:space:]]") ; SRE
205 (whitespace . space
) ; SRE
206 (white . space
) ; SRE
207 (upper .
"[[:upper:]]") ; SRE
208 (upper-case . upper
) ; SRE
209 (word .
"[[:word:]]") ; inconsistent with SRE
210 (wordchar . word
) ; sregex
211 (not-wordchar .
"[^[:word:]]") ; sregex (use \\W?)
213 "Alist of sexp form regexp constituents.
214 Each element of the alist has the form (SYMBOL . DEFN).
215 SYMBOL is a valid constituent of sexp regular expressions.
216 If DEFN is a string, SYMBOL is translated into DEFN.
217 If DEFN is a symbol, use the definition of DEFN, recursively.
218 Otherwise, DEFN must be a list (FUNCTION MIN-ARGS MAX-ARGS PREDICATE).
219 FUNCTION is used to produce code for SYMBOL. MIN-ARGS and MAX-ARGS
220 are the minimum and maximum number of arguments the function-form
221 sexp constituent SYMBOL may have in sexp regular expressions.
222 MAX-ARGS nil means no limit. PREDICATE, if specified, means that
223 all arguments must satisfy PREDICATE.")
231 (open-parenthesis . ?\
()
232 (close-parenthesis . ?\
))
233 (expression-prefix . ?
\')
235 (paired-delimiter . ?$
)
237 (character-quote . ?
/)
240 (string-delimiter . ?|
)
241 (comment-delimiter . ?
!))
242 "Alist mapping Rx syntax symbols to syntax characters.
243 Each entry has the form (SYMBOL . CHAR), where SYMBOL is a valid
244 symbol in `(syntax SYMBOL)', and CHAR is the syntax character
245 corresponding to SYMBOL, as it would be used with \\s or \\S in
246 regular expressions.")
249 (defconst rx-categories
252 (upper-diacritical-mark . ?
2)
253 (lower-diacritical-mark . ?
3)
257 (vowel-modifying-diacritical-mark . ?
7)
259 (semivowel-lower . ?
9)
260 (not-at-end-of-line . ?
<)
261 (not-at-beginning-of-line . ?
>)
262 (alpha-numeric-two-byte . ?A
)
263 (chinse-two-byte . ?C
)
264 (greek-two-byte . ?G
)
265 (japanese-hiragana-two-byte . ?H
)
266 (indian-two-byte . ?I
)
267 (japanese-katakana-two-byte . ?K
)
268 (korean-hangul-two-byte . ?N
)
269 (cyrillic-two-byte . ?Y
)
270 (combining-diacritic . ?^
)
279 (japanese-katakana . ?k
)
283 (japanese-roman . ?r
)
289 "Alist mapping symbols to category characters.
290 Each entry has the form (SYMBOL . CHAR), where SYMBOL is a valid
291 symbol in `(category SYMBOL)', and CHAR is the category character
292 corresponding to SYMBOL, as it would be used with `\\c' or `\\C' in
293 regular expression strings.")
296 (defvar rx-greedy-flag t
297 "Non-nil means produce greedy regular expressions for `zero-or-one',
298 `zero-or-more', and `one-or-more'. Dynamically bound.")
302 "Return parsing/code generation info for OP.
303 If OP is the space character ASCII 32, return info for the symbol `?'.
304 If OP is the character `?', return info for the symbol `??'.
305 See also `rx-constituents'."
306 (cond ((eq op ?
) (setq op
'\?))
307 ((eq op ??
) (setq op
'\??
)))
308 (while (and (not (null op
)) (symbolp op
))
309 (setq op
(cdr (assq op rx-constituents
))))
313 (defun rx-check (form)
314 "Check FORM according to its car's parsing info."
316 (error "rx `%s' needs argument(s)" form
))
317 (let* ((rx (rx-info (car form
)))
318 (nargs (1- (length form
)))
319 (min-args (nth 1 rx
))
320 (max-args (nth 2 rx
))
321 (type-pred (nth 3 rx
)))
322 (when (and (not (null min-args
))
324 (error "rx form `%s' requires at least %d args"
325 (car form
) min-args
))
326 (when (and (not (null max-args
))
328 (error "rx form `%s' accepts at most %d args"
329 (car form
) max-args
))
330 (when (not (null type-pred
))
331 (dolist (sub-form (cdr form
))
332 (unless (funcall type-pred sub-form
)
333 (error "rx form `%s' requires args satisfying `%s'"
334 (car form
) type-pred
))))))
338 "Parse and produce code from FORM.
339 FORM is of the form `(and FORM1 ...)'."
343 (function (lambda (x) (rx-to-string x
'no-group
)))
349 "Parse and produce code from FORM, which is `(or FORM1 ...)'."
351 (let ((all-args-strings t
))
352 (dolist (arg (cdr form
))
353 (unless (stringp arg
)
354 (setq all-args-strings nil
)))
357 (regexp-opt (cdr form
))
358 (mapconcat #'rx-to-string
(cdr form
) "\\|"))
362 (defvar rx-bracket
) ; dynamically bound in `rx-any'
364 (defun rx-check-any (arg)
365 "Check arg ARG for Rx `any'."
367 (setq arg
(string arg
)))
369 (if (zerop (length arg
))
370 (error "String arg for Rx `any' must not be empty"))
371 ;; Quote ^ at start; don't bother to check whether this is first arg.
372 (if (eq ?^
(aref arg
0))
373 (setq arg
(concat "\\" arg
)))
374 ;; Remove ] and set flag for adding it to start of overall result.
375 (when (string-match "\\]" arg
)
376 (setq arg
(replace-regexp-in-string "\\]" "" arg
)
379 (let ((translation (condition-case nil
380 (rx-to-string arg
'no-group
)
382 (unless translation
(error "Invalid char class `%s' in Rx `any'" arg
))
383 (setq arg
(substring translation
1 -
1)))) ; strip outer brackets
384 ;; sregex compatibility
385 (when (and (integerp (car-safe arg
))
386 (integerp (cdr-safe arg
)))
387 (setq arg
(string (car arg
) ?-
(cdr arg
))))
388 (unless (stringp arg
)
389 (error "rx `any' requires string, character, char pair or char class args"))
393 "Parse and produce code from FORM, which is `(any ARG ...)'.
396 (let* ((rx-bracket nil
)
397 (args (mapcar #'rx-check-any
(cdr form
)))) ; side-effects `rx-bracket'
398 ;; If there was a ?- in the form, move it to the front to avoid
400 (if (member "-" args
)
401 (setq args
(cons "-" (delete "-" args
))))
402 (apply #'concat
"[" rx-bracket
(append args
'("]")))))
405 (defun rx-check-not (arg)
406 "Check arg ARG for Rx `not'."
407 (unless (or (and (symbolp arg
)
408 (string-match "\\`\\[\\[:[-a-z]:\\]\\]\\'"
410 (rx-to-string arg
'no-group
)
412 (eq arg
'word-boundary
)
414 (memq (car arg
) '(not any in syntax category
))))
415 (error "rx `not' syntax error: %s" arg
))
420 "Parse and produce code from FORM. FORM is `(not ...)'."
422 (let ((result (rx-to-string (cadr form
) 'no-group
))
424 (cond ((string-match "\\`\\[^" result
)
425 (if (= (length result
) 4)
426 (substring result
2 3)
427 (concat "[" (substring result
2))))
428 ((eq ?\
[ (aref result
0))
429 (concat "[^" (substring result
1)))
430 ((string-match "\\`\\\\[scb]" result
)
431 (concat (capitalize (substring result
0 2)) (substring result
2)))
433 (concat "[^" result
"]")))))
436 (defun rx-not-char (form)
437 "Parse and produce code from FORM. FORM is `(not-char ...)'."
439 (rx-not `(not (in ,@(cdr form
)))))
442 (defun rx-not-syntax (form)
443 "Parse and produce code from FORM. FORM is `(not-syntax SYNTAX)'."
445 (rx-not `(not (syntax ,@(cdr form
)))))
448 (defun rx-trans-forms (form &optional skip
)
449 "If FORM's length is greater than two, transform it to length two.
450 A form (HEAD REST ...) becomes (HEAD (and REST ...)).
451 If SKIP is non-nil, allow that number of items after the head, i.e.
452 `(= N REST ...)' becomes `(= N (and REST ...))' if SKIP is 1."
453 (unless skip
(setq skip
0))
454 (let ((tail (nthcdr (1+ skip
) form
)))
455 (if (= (length tail
) 1)
457 (let ((form (copy-sequence form
)))
458 (setcdr (nthcdr skip form
) (list (cons 'and tail
)))
463 "Parse and produce code from FORM `(= N ...)'."
465 (setq form
(rx-trans-forms form
1))
466 (unless (and (integerp (nth 1 form
))
468 (error "rx `=' requires positive integer first arg"))
469 (format "%s\\{%d\\}" (rx-to-string (nth 2 form
)) (nth 1 form
)))
473 "Parse and produce code from FORM `(>= N ...)'."
475 (setq form
(rx-trans-forms form
1))
476 (unless (and (integerp (nth 1 form
))
478 (error "rx `>=' requires positive integer first arg"))
479 (format "%s\\{%d,\\}" (rx-to-string (nth 2 form
)) (nth 1 form
)))
483 "Parse and produce code from FORM `(** N M ...)'."
485 (setq form
(cons 'repeat
(cdr (rx-trans-forms form
2))))
489 (defun rx-repeat (form)
490 "Parse and produce code from FORM.
491 FORM is either `(repeat N FORM1)' or `(repeat N M FORM1)'."
493 (cond ((= (length form
) 3)
494 (unless (and (integerp (nth 1 form
))
496 (error "rx `repeat' requires positive integer first arg"))
497 (format "%s\\{%d\\}" (rx-to-string (nth 2 form
)) (nth 1 form
)))
498 ((or (not (integerp (nth 2 form
)))
500 (not (integerp (nth 1 form
)))
502 (< (nth 2 form
) (nth 1 form
)))
503 (error "rx `repeat' range error"))
505 (format "%s\\{%d,%d\\}" (rx-to-string (nth 3 form
))
506 (nth 1 form
) (nth 2 form
)))))
509 (defun rx-submatch (form)
510 "Parse and produce code from FORM, which is `(submatch ...)'."
512 (mapconcat (function (lambda (x) (rx-to-string x
'no-group
)))
516 (defun rx-backref (form)
517 "Parse and produce code from FORM, which is `(backref N)'."
519 (format "\\%d" (nth 1 form
)))
521 (defun rx-check-backref (arg)
522 "Check arg ARG for Rx `backref'."
523 (or (and (integerp arg
) (>= arg
1) (<= arg
9))
524 (error "rx `backref' requires numeric 1<=arg<=9: %s" arg
)))
526 (defun rx-kleene (form)
527 "Parse and produce code from FORM.
528 FORM is `(OP FORM1)', where OP is one of the `zero-or-one',
529 `zero-or-more' etc. operators.
530 If OP is one of `*', `+', `?', produce a greedy regexp.
531 If OP is one of `*?', `+?', `??', produce a non-greedy regexp.
532 If OP is anything else, produce a greedy regexp if `rx-greedy-flag'
535 (setq form
(rx-trans-forms form
))
536 (let ((suffix (cond ((memq (car form
) '(* + ?
)) "")
537 ((memq (car form
) '(*?
+? ??
)) "?")
540 (op (cond ((memq (car form
) '(* *?
0+ zero-or-more
)) "*")
541 ((memq (car form
) '(+ +?
1+ one-or-more
)) "+")
543 (result (rx-to-string (cadr form
) 'no-group
)))
544 (if (not (rx-atomic-p result
))
545 (setq result
(concat "\\(?:" result
"\\)")))
546 (concat result op suffix
)))
548 (defun rx-atomic-p (r)
549 "Return non-nil if regexp string R is atomic.
550 An atomic regexp R is one such that a suffix operator
551 appended to R will apply to all of R. For example, \"a\"
552 \"[abc]\" and \"\\(ab\\|ab*c\\)\" are atomic and \"ab\",
553 \"[ab]c\", and \"ab\\|ab*c\" are not atomic.
555 This function may return false negatives, but it will not
556 return false positives. It is nevertheless useful in
557 situations where an efficiency shortcut can be taken iff a
558 regexp is atomic. The function can be improved to detect
559 more cases of atomic regexps. Presently, this function
560 detects the following categories of atomic regexp;
562 a group or shy group: \\(...\\)
563 a character class: [...]
564 a single character: a
566 On the other hand, false negatives will be returned for
567 regexps that are atomic but end in operators, such as
568 \"a+\". I think these are rare. Probably such cases could
569 be detected without much effort. A guarantee of no false
570 negatives would require a theoretic specification of the set
571 of all atomic regexps."
572 (let ((l (length r
)))
575 (equal (substring r
0 2) "\\(")
576 (equal (substring r -
2) "\\)"))
578 (equal (substring r
0 1) "[")
579 (equal (substring r -
1) "]")))))
582 (defun rx-syntax (form)
583 "Parse and produce code from FORM, which is `(syntax SYMBOL)'."
585 (let* ((sym (cadr form
))
586 (syntax (assq sym rx-syntax
)))
588 ;; Try sregex compatibility.
589 (let ((name (symbol-name sym
)))
590 (if (= 1 (length name
))
591 (setq syntax
(rassq (aref name
0) rx-syntax
))))
593 (error "Unknown rx syntax `%s'" (cadr form
))))
594 (format "\\s%c" (cdr syntax
))))
597 (defun rx-check-category (form)
598 "Check the argument FORM of a `(category FORM)'."
599 (unless (or (integerp form
)
600 (cdr (assq form rx-categories
)))
601 (error "Unknown category `%s'" form
))
605 (defun rx-category (form)
606 "Parse and produce code from FORM, which is `(category SYMBOL)'."
608 (let ((char (if (integerp (cadr form
))
610 (cdr (assq (cadr form
) rx-categories
)))))
611 (format "\\c%c" char
)))
614 (defun rx-eval (form)
615 "Parse and produce code from FORM, which is `(eval FORM)'."
617 (rx-to-string (eval (cadr form
))))
620 (defun rx-greedy (form)
621 "Parse and produce code from FORM.
622 If FORM is '(minimal-match FORM1)', non-greedy versions of `*',
623 `+', and `?' operators will be used in FORM1. If FORM is
624 '(maximal-match FORM1)', greedy operators will be used."
626 (let ((rx-greedy-flag (eq (car form
) 'maximal-match
)))
627 (rx-to-string (cadr form
))))
630 (defun rx-regexp (form)
631 "Parse and produce code from FORM, which is `(regexp STRING)'."
633 (concat "\\(?:" (cadr form
) "\\)"))
637 (defun rx-to-string (form &optional no-group
)
638 "Parse and produce code for regular expression FORM.
639 FORM is a regular expression in sexp form.
640 NO-GROUP non-nil means don't put shy groups around the result."
641 (cond ((stringp form
)
644 (regexp-quote (char-to-string form
)))
646 (let ((info (rx-info form
)))
647 (cond ((stringp info
)
650 (error "Unknown rx form `%s'" form
))
652 (funcall (nth 0 info
) form
)))))
654 (let ((info (rx-info (car form
))))
656 (error "Unknown rx form `%s'" (car form
)))
657 (let ((result (funcall (nth 0 info
) form
)))
658 (if (or no-group
(string-match "\\`\\\\[(]" result
))
660 (concat "\\(?:" result
"\\)")))))
662 (error "rx syntax error at `%s'" form
))))
666 (defmacro rx
(&rest regexps
)
667 "Translate regular expressions REGEXPS in sexp form to a regexp string.
668 REGEXPS is a non-empty sequence of forms of the sort listed below.
669 See also `rx-to-string' for how to do such a translation at run-time.
671 The following are valid subforms of regular expressions in sexp
675 matches string STRING literally.
678 matches character CHAR literally.
680 `not-newline', `nonl'
681 matches any character except a newline.
684 matches any character
689 matches any character in SET .... SET may be a character or string.
690 Ranges of characters can be specified as `A-Z' in strings.
691 Ranges may also be specified as conses like `(?A . ?Z)'.
693 SET may also be the name of a character class: `digit',
694 `control', `hex-digit', `blank', `graph', `print', `alnum',
695 `alpha', `ascii', `nonascii', `lower', `punct', `space', `upper',
696 `word', or one of their synonyms.
698 `(not (any SET ...))'
699 matches any character not in SET ...
702 matches the empty string, but only at the beginning of a line
703 in the text being matched
706 is similar to `line-start' but matches only at the end of a line
708 `string-start', `bos', `bot'
709 matches the empty string, but only at the beginning of the
710 string being matched against.
712 `string-end', `eos', `eot'
713 matches the empty string, but only at the end of the
714 string being matched against.
717 matches the empty string, but only at the beginning of the
718 buffer being matched against. Actually equivalent to `string-start'.
721 matches the empty string, but only at the end of the
722 buffer being matched against. Actually equivalent to `string-end'.
725 matches the empty string, but only at point.
728 matches the empty string, but only at the beginning or end of a
732 matches the empty string, but only at the end of a word.
735 matches the empty string, but only at the beginning or end of a
738 `(not word-boundary)'
740 matches the empty string, but not at the beginning or end of a
743 `digit', `numeric', `num'
747 matches ASCII control characters.
749 `hex-digit', `hex', `xdigit'
750 matches 0 through 9, a through f and A through F.
753 matches space and tab only.
756 matches graphic characters--everything except ASCII control chars,
760 matches printing characters--everything except ASCII control chars
763 `alphanumeric', `alnum'
764 matches letters and digits. (But at present, for multibyte characters,
765 it matches anything that has word syntax.)
767 `letter', `alphabetic', `alpha'
768 matches letters. (But at present, for multibyte characters,
769 it matches anything that has word syntax.)
772 matches ASCII (unibyte) characters.
775 matches non-ASCII (multibyte) characters.
777 `lower', `lower-case'
778 matches anything lower-case.
780 `upper', `upper-case'
781 matches anything upper-case.
783 `punctuation', `punct'
784 matches punctuation. (But at present, for multibyte characters,
785 it matches anything that has non-word syntax.)
787 `space', `whitespace', `white'
788 matches anything that has whitespace syntax.
791 matches anything that has word syntax.
794 matches anything that has non-word syntax.
797 matches a character with syntax SYNTAX. SYNTAX must be one
798 of the following symbols, or a symbol corresponding to the syntax
799 character, e.g. `\\.' for `\\s.'.
801 `whitespace' (\\s- in string notation)
805 `open-parenthesis' (\\s()
806 `close-parenthesis' (\\s))
807 `expression-prefix' (\\s')
808 `string-quote' (\\s\")
809 `paired-delimiter' (\\s$)
811 `character-quote' (\\s/)
812 `comment-start' (\\s<)
814 `string-delimiter' (\\s|)
815 `comment-delimiter' (\\s!)
817 `(not (syntax SYNTAX))'
818 matches a character that doesn't have syntax SYNTAX.
820 `(category CATEGORY)'
821 matches a character with category CATEGORY. CATEGORY must be
822 either a character to use for C, or one of the following symbols.
824 `consonant' (\\c0 in string notation)
826 `upper-diacritical-mark' (\\c2)
827 `lower-diacritical-mark' (\\c3)
831 `vowel-modifying-diacritical-mark' (\\c7)
833 `semivowel-lower' (\\c9)
834 `not-at-end-of-line' (\\c<)
835 `not-at-beginning-of-line' (\\c>)
836 `alpha-numeric-two-byte' (\\cA)
837 `chinse-two-byte' (\\cC)
838 `greek-two-byte' (\\cG)
839 `japanese-hiragana-two-byte' (\\cH)
840 `indian-tow-byte' (\\cI)
841 `japanese-katakana-two-byte' (\\cK)
842 `korean-hangul-two-byte' (\\cN)
843 `cyrillic-two-byte' (\\cY)
844 `combining-diacritic' (\\c^)
853 `japanese-katakana' (\\ck)
857 `japanese-roman' (\\cr)
864 `(not (category CATEGORY))'
865 matches a character that doesn't have category CATEGORY.
867 `(and SEXP1 SEXP2 ...)'
868 `(: SEXP1 SEXP2 ...)'
869 `(seq SEXP1 SEXP2 ...)'
870 `(sequence SEXP1 SEXP2 ...)'
871 matches what SEXP1 matches, followed by what SEXP2 matches, etc.
873 `(submatch SEXP1 SEXP2 ...)'
874 `(group SEXP1 SEXP2 ...)'
875 like `and', but makes the match accessible with `match-end',
876 `match-beginning', and `match-string'.
878 `(group SEXP1 SEXP2 ...)'
879 another name for `submatch'.
881 `(or SEXP1 SEXP2 ...)'
882 `(| SEXP1 SEXP2 ...)'
883 matches anything that matches SEXP1 or SEXP2, etc. If all
884 args are strings, use `regexp-opt' to optimize the resulting
887 `(minimal-match SEXP)'
888 produce a non-greedy regexp for SEXP. Normally, regexps matching
889 zero or more occurrences of something are \"greedy\" in that they
890 match as much as they can, as long as the overall regexp can
891 still match. A non-greedy regexp matches as little as possible.
893 `(maximal-match SEXP)'
894 produce a greedy regexp for SEXP. This is the default.
896 Below, `SEXP ...' represents a sequence of regexp forms, treated as if
897 enclosed in `(and ...)'.
899 `(zero-or-more SEXP ...)'
901 matches zero or more occurrences of what SEXP ... matches.
904 like `zero-or-more', but always produces a greedy regexp, independent
908 like `zero-or-more', but always produces a non-greedy regexp,
909 independent of `rx-greedy-flag'.
911 `(one-or-more SEXP ...)'
913 matches one or more occurrences of SEXP ...
916 like `one-or-more', but always produces a greedy regexp.
919 like `one-or-more', but always produces a non-greedy regexp.
921 `(zero-or-one SEXP ...)'
922 `(optional SEXP ...)'
924 matches zero or one occurrences of A.
927 like `zero-or-one', but always produces a greedy regexp.
930 like `zero-or-one', but always produces a non-greedy regexp.
934 matches N occurrences.
937 matches N or more occurrences.
941 matches N to M occurrences.
944 matches what was matched previously by submatch N.
947 matches what was matched previously by submatch N.
950 matches what was matched previously by submatch N.
953 evaluate FORM and insert result. If result is a string,
957 include REGEXP in string notation in the result."
958 (cond ((null regexps
)
961 (rx-to-string `(and ,@regexps
) t
))
963 (rx-to-string (car regexps
) t
))))
965 ;; ;; sregex.el replacement
967 ;; ;;;###autoload (provide 'sregex)
968 ;; ;;;###autoload (autoload 'sregex "rx")
969 ;; (defalias 'sregex 'rx-to-string)
970 ;; ;;;###autoload (autoload 'sregexq "rx" nil nil 'macro)
971 ;; (defalias 'sregexq 'rx)
975 ;; arch-tag: 12d01a63-0008-42bb-ab8c-1c7d63be370b