******************************* The regular-expressions Library ******************************* .. current-library:: regular-expressions .. current-module:: regular-expressions Overview ======== The ``regular-expressions`` library exports the ``regular-expressions`` module, which contains functions that compile and search for regular expressions. The module has the same semantics as Perl (version 4) unless otherwise noted. A regular expression that is grammatically correct may still be illegal if it contains an infinitely quantified sub-regex that matches the empty string. That is, if R is a regex that can match the empty string, then any regex containing R*, R+, and R{n,} is illegal. In this case, the ``regular-expressions`` library will signal an :class:`` error when the regex is parsed. Quick Start =========== The most common use of regular expressions is probably to perform a search and figure out what text matched and/or where it occurred in a string. You need to ``use regular-expressions;`` in both your library and your module, and then... .. code-block:: dylan define constant $re :: = compile-regex("^abc(.+)123$"); let match :: false-or() = regex-search($re, "abcdef123"); // match is #f if search failed. if (match) let text = match-group(match, 1); // text = "def" let (text, start, _end) = match-group(match, 1); // text = "def", start = 3, _end = 6 match-group(match, 2) => error: // group 0 is the entire match ... end; compile-regex("*") => error: Reference ========= .. class:: :sealed: A compiled regular expression object. These should only be created via :func:`compile-regex`. .. class:: :sealed: The superclass of all regular expression-related errors. :superclasses: :class:``, :drm:`` .. class:: :sealed: Signalled by :func:`compile-regex` when the given regular expression cannot be compiled. :superclasses: :class:`` :keyword pattern: .. generic-function:: regex-error-pattern :sealed: Return the pattern that caused an :class:`` error. :signature: regex-error-pattern *error* => *pattern* :parameter error: An :class:``. :value pattern: A :drm:``. .. class:: :sealed: Signalled when an invalid group identifier is passed to :func:`match-group`. :superclasses: :class:`` .. class:: :sealed: Stores the match groups and other information about a specific regex search result. :superclasses: :drm:`` :keyword regular-expression: .. generic-function:: compile-regex :sealed: Compile a string into a :class:``. :signature: compile-regex *pattern* #key *case-sensitive* *verbose* *multi-line* *dot-matches-all* *use-cache* => *regex* :parameter pattern: A :drm:``. :parameter #key case-sensitive: A :drm:``, default ``#t``. :parameter #key verbose: A :drm:``, default ``#f``. :parameter #key multi-line: A :drm:``, default ``#f``. :parameter #key dot-matches-all: A :drm:``, default ``#f``. :parameter #key use-cache: A :drm:``, default ``#t``. If true, the resulting regular expression will be cached and re-used the next time the same string is compiled. :value regex: A :class:``. :conditions: :class:`` is signalled if *pattern* can't be compiled. .. generic-function:: regex-pattern :sealed: Return the :drm:`` from which *regex* was created. :signature: regex-pattern *regex* => *pattern* :parameter regex: A :class:``. :value pattern: A :drm:``. .. generic-function:: regex-group-count :sealed: Return the number of groups in a :class:``. :signature: regex-group-count *regex* => *num-groups* :parameter regex: A :class:``. :value num-groups: An :drm:``. .. generic-function:: regex-position :sealed: Find the position of *pattern* in *text*. :signature: regex-position *pattern* *text* #key *start* *end* *case-sensitive* => *regex-start*, #rest *marks* :parameter pattern: A :class:``. :parameter text: A :drm:``. :parameter #key start: A :drm:``, default ``0``. The index at which to start the search. :parameter #key end: An :drm:``, default ``*text*.size``. The index at which to end the search. :parameter #key case-sensitive: A :drm:``, default ``#t``. :value regex-start: An instance of ``false-or()``. :value #rest marks: An instance of :drm:``. A match will only be found if it fits entirely within the range specified by *start* and *end*. If the regular expression is not found, return #f, otherwise return a variable number of indices marking the start and end of groups. This is a low-level API. Use :func:`regex-search` if you want to get a :class:`` object back. .. generic-function:: regex-replace :sealed: Replace occurrences of *pattern* within *big* with *replacement*. :signature: regex-replace *big* *pattern* *replacement* #key *start* *end* *count* *case-sensitive* => *new-string* :parameter big: The :drm:`` within which to search. :parameter pattern: The :class:`` to search for. :parameter replacement: The :drm:`` to replace *pattern* with. :parameter #key start: An :drm:``, default ``0``. The index in *big* at which to start searching. :parameter #key end: An :drm:``, default ``*big*.size``. The index at which to end the search. :parameter #key case-sensitive: A :drm:``, default ``#t``. :parameter #key count: An instance of ``false-or()``, default ``#f``. The number of matches to replace. ``#f`` means to replace all. :value new-string: An instance of :drm:``. A match will only be found if it fits entirely within the range specified by *start* and *end*. .. generic-function:: regex-search :sealed: Search for a *pattern* within *text*. :signature: regex-search *pattern* *text* #key *anchored* *start* *end* *case-sensitive* => *match* :parameter pattern: The :class:`` to search for. :parameter text: The :drm:`` in which to search. :parameter #key anchored: A :drm:``, default ``#f``. Whether or not the search should be anchored at the start position. This is useful because "^..." will only match at the beginning of a string, or after \\n if the regex was compiled with multi-line = #t. :parameter #key start: An :drm:``, default ``0``. The index in *text* at which to start searching. :parameter #key end: An :drm:``, default ``*text*.size``. The index at which to end the search. :parameter #key case-sensitive: A :drm:``, default ``#t``. :value match: An instance of ``false-or()``. ``#f`` is returned if no match was found. A match will only be found if it fits entirely within the range specified by *start* and *end*. .. generic-function:: regex-search-strings :sealed: Find all matches for a regular expression within a string. :signature: regex-search-strings *pattern* *text* #key *anchored* *start* *end* *case-sensitive* => #rest *strings* :parameter pattern: An instance of :class:``. :parameter text: An instance of :drm:``. :parameter #key anchored: An instance of :drm:``. :parameter #key start: An :drm:``, default ``0``. The index in *text* at which to start searching. :parameter #key end: An :drm:``, default ``*text*.size``. The index at which to end the search. :parameter #key case-sensitive: A :drm:``, default ``#t``. :value #rest strings: An instance of :drm:``. A match will only be found if it fits entirely within the range specified by *start* and *end*. .. generic-function:: match-group :sealed: Return information about a specific match group in a :class:``. :signature: match-group *match* *group* => *text* *start-index* *end-index* :parameter match: An instance of :class:``. :parameter group: An instance of :drm:`` or :drm:``. :value text: An instance of ``false-or()``. :value start-index: An instance of ``false-or()``. :value end-index: An instance of ``false-or()``. :conditions: :class:`` is signalled if ``group`` does not name a valid group. The requested group may be an :drm:`` to access groups by number, or a :drm:`` to access groups by name. Accessing groups by name only works if they were given names in the compiled regular expression via the ``(?...)`` syntax. Group 0 is always the entire regular expression match. It is possible for the group identifier to be valid and for ``#f`` to be returned. This can happen, for example, if the group was in the part of an ``|`` (or) expression that didn't match.