(irregex-search '(: "<" (>= 3 (~ #\>)) ">") "") => #
(irregex-search '(: "<" (>= 3 (~ #\>)) ">") "") => #f
To match a specific number of times exactly, use {{=}}:
(irregex-search '(: "<" (= 4 (~ #\>)) ">") "") => #
(irregex-search '(: "<" (= 4 (~ #\>)) ">") "") => #f
And finally, the most general form is {{**}} which specifies a range
of times to match. All of the earlier forms are special cases of this.
(irregex-search '(: (= 3 (** 1 3 numeric) ".") (** 1 3 numeric)) "192.168.1.10") => #
(irregex-search '(: (= 3 (** 1 3 numeric) ".") (** 1 3 numeric)) "192.0168.1.10") => #f
There are also so-called "non-greedy" variants of these repetition
operators, by convention suffixed with an additional {{?}}. Since the
normal repetition patterns can match any of the allotted repetition
range, these operators will match a string if and only if the normal
versions matched. However, when the endpoints of which submatch
matched where are taken into account (specifically, all matches when
using irregex-search since the endpoints of the match itself matter),
the use of a non-greedy repetition can change the result.
So, whereas {{?}} can be thought to mean "match or don't match,"
{{??}} means "don't match or match." {{*}} typically consumes as much
as possible, but {{*?}} tries first to match zero times, and only
consumes one at a time if that fails. If you have a greedy operator
followed by a non-greedy operator in the same pattern, they can
produce surprisins results as they compete to make the match longer or
shorter. If this seems confusing, that's because it is. Non-greedy
repetitions are defined only in terms of the specific backtracking
algorithm used to implement them, which for compatibility purposes
always means the Perl algorithm. Thus, when using these patterns you
force IrRegex to use a backtracking engine, and can't rely on
efficient execution.
===== SRE Character Sets
Perhaps more common than matching specific strings is matching any of
a set of characters. You can use the {{or}} alternation pattern on a
list of single-character strings to simulate a character set, but this
is too clumsy for everyday use so SRE syntax allows a number of
shortcuts.
A single character matches that character literally, a trivial
character class. More conveniently, a list holding a single element
which is a string refers to the character set composed of every
character in the string.
(irregex-match '(* #\-) "---") => #
(irregex-match '(* #\-) "-_-") => #f
(irregex-match '(* ("aeiou")) "oui") => #
(irregex-match '(* ("aeiou")) "ouais") => #f
Ranges are introduced with the \q{/} operator. Any strings or
characters in the \q{/} are flattened and then taken in pairs to
represent the start and end points, inclusive, of character ranges.
(irregex-match '(* (/ "AZ09")) "R2D2") => #
(irregex-match '(* (/ "AZ09")) "C-3PO") => #f
In addition, a number of set algebra operations are provided. \q{or},
of course, has the same meaning, but when all the options are
character sets it can be thought of as the set union operator. This
is further extended by the \q{&} set intersection, \q{-} set
difference, and \q{~} set complement operators.
(irregex-match '(* (& (/ "az") (~ ("aeiou")))) "xyzzy") => #
(irregex-match '(* (& (/ "az") (~ ("aeiou")))) "vowels") => #f
(irregex-match '(* (- (/ "az") ("aeiou"))) "xyzzy") => #
(irregex-match '(* (- (/ "az") ("aeiou"))) "vowels") => #f
===== SRE Assertion Patterns
There are a number of times it can be useful to assert something about
the area around a pattern without explicitly making it part of the
pattern. The most common cases are specifically anchoring some
pattern to the beginning or end of a word or line or even the whole
string. For example, to match on the end of a word:
(irregex-match '(: "foo" eow) "foo") => #
(irregex-match '(: "foo" eow) "foo!") => #
(irregex-match '(: "foo" eow) "foof") => #f
The {{bow}}, {{bol}}, {{eol}}, {{bos}} and {{eos}} work similarly.
{{nwb}} asserts that you are not in a word-boundary - if replaced for
{{eow}} in the above examples it would reverse all the results.
There is no {{wb}}, since you tend to know from context whether it
would be the beginning or end of a word, but if you need it you can
always use {{(or bow eow)}}.
Somewhat more generally, Perl introduced positive and negative
look-ahead and look-behind patterns. Perl look-behind patterns are
limited to a fixed length, however the IrRegex versions have no such
limit.
(irregex-match '(: "regular" (look-ahead " expression"))
"regular expression")
=> #
The most general case, of course, would be an \q{and} pattern to
complement the \q{or} pattern - all the patterns must match or the
whole pattern fails. This may be provided in a future release,
although it (and look-ahead and look-behind assertions) are unlikely
to be compiled efficiently.
===== SRE Utility Patterns
The following utility regular expressions are also provided for common
patterns that people are eternally reinventing. They are not
necessarily the official patterns matching the RFC definitions of the
given data, because of the way that such patterns tend to be used.
There are three general usages for regexps:
; searching : search for a pattern matching a desired object in a larger text
; validation : determine whether an entire string matches a pattern
; extraction : given a string already known to be valid, extract certain fields from it as submatches
In some cases, but not always, these will overlap. When they are
different, {{irregex-search}} will naturally always want the searching
version, so IrRegex provides that version.
As an example where these might be different, consider a URL. If you
want to match all the URLs in some arbitrary text, you probably want
to exclude a period or comma at the tail end of a URL, since it's more
likely being used as punctuation rather than part of the URL, despite
the fact that it would be valid URL syntax.
Another problem with the RFC definitions is the standard itself may
have become irrelevant. For example, the pattern IrRegex provides for
email addresses doesn't match quoted local parts (e.g.
{{"first last"@domain.com}}) because these are increasingly rare, and
unsupported by enough software that it's better to discourage their use.
Conversely, technically consecutive periods
(e.g. {{first..last@domain.com}}) are not allowed in email addresses, but
most email software does allow this, and in fact such addresses are
quite common in Japan.
The current patterns provided are:
newline ; general newline pattern (crlf, cr, lf)
integer ; an integer
real ; a real number (including scientific)
string ; a "quoted" string
symbol ; an R5RS Scheme symbol
ipv4-address ; a numeric decimal ipv4 address
ipv6-address ; a numeric hexadecimal ipv6 address
domain ; a domain name
email ; an email address
http-url ; a URL beginning with https?://
Because of these issues the exact definitions of these patterns are
subject to be changed, but will be documented clearly when they are
finalized. More common patterns are also planned, but as what you
want increases in complexity it's probably better to use a real
parser.
==== Supported PCRE Syntax
Since the PCRE syntax is so overwhelming complex, it's easier to just
list what we *don't* support for now. Refer to the
[[http://pcre.org/pcre.txt|PCRE documentation]] for details. You
should be using the SRE syntax anyway!
Unicode character classes ({{\P}}) are not supported, but will be
in an upcoming release. {{\C}} named characters are not supported.
Callbacks, subroutine patterns and recursive patterns are not
supported. ({{*FOO}}) patterns are not supported and may never be.
{{\G}} and {{\K}} are not supported.
Octal character escapes are not supported because they are ambiguous
with back-references - just use hex character escapes.
Other than that everything should work, including named submatches,
zero-width assertions, conditional patterns, etc.
In addition, {{\<}} and {{\>}} act as beginning-of-word and end-of-word
marks, respectively, as in Emacs regular expressions.
Also, two escapes are provided to embed SRE patterns inside PCRE
strings, {{"\'"}} and {{"(*')"}}. For example, to match a
comma-delimited list of integers you could use
"\\'integer(,\\'integer)*"
and to match a URL in angle brackets you could use
"<('*http-url)>"
Note in the second example the enclosing {{"('*...)"}} syntax is needed
because the Scheme reader would consider the closing {{">"}} as part of
the SRE symbol.
The following chart gives a quick reference from PCRE form to the SRE
equivalent:
;; basic syntax
"^" ;; bos (or eos inside (?m: ...))
"$" ;; eos (or eos inside (?m: ...))
"." ;; nonl
"a?" ;; (? a)
"a*" ;; (* a)
"a+" ;; (+ a)
"a??" ;; (?? a)
"a*?" ;; (*? a)
"a+?" ;; (+? a)
"a{n,m}" ;; (** n m a)
;; grouping
"(...)" ;; (submatch ...)
"(?:...)" ;; (: ...)
"(?i:...)" ;; (w/nocase ...)
"(?-i:...)" ;; (w/case ...)
"(?...)" ;; (=> ...)
;; character classes
"[aeiou]" ;; ("aeiou")
"[^aeiou]" ;; (~ "aeiou")
"[a-z]" ;; (/ "az") or (/ "a" "z")
"[[:alpha:]]" ;; alpha
;; assertions
"(?=...)" ;; (look-ahead ...)
"(?!...)" ;; (neg-look-ahead ...)
"(?<=...)" ;; (look-behind ...)
"(?(make-irregex-chunker [ ])
where
{{( chunk) => }} returns the next chunk, or {{#f}} if there are no more chunks
{{( chunk) => }} a string source for the chunk
{{( chunk) => }} the start index of the result of {{}} (defaults to always 0)
{{( chunk) => }} the end (exclusive) of the string (defaults to {{string-length}} of the source string)
{{( cnk1 i cnk2 j) => }} a substring for the range between the chunk {{cnk1}} starting at index {{i}} and ending at {{cnk2}} at index {{j}}
{{( cnk1 i cnk2 j) => }} as above but returns a new chunked data type instead of a string (optional)
There are two important constraints on the {{}} procedure.
It must return an {{eq?}} identical object when called multiple times
on the same chunk, and it must not return a chunk with an empty string
(start == end). This second constraint is for performance reasons -
we push the work of possibly filtering empty chunks to the chunker
since there are many chunk types for which empty strings aren't
possible, and this work is thus not needed. Note that the initial
chunk passed to match on is allowed to be empty.
{{}} is provided for possible performance improvements
- without it a default is used. {{}} is optional -
without it you may not use {{irregex-match-subchunk}} described above.
You can then match chunks of these types with the following
procedures:
===== irregex-search/chunked
===== irregex-match/chunked
(irregex-search/chunked [])
(irregex-match/chunked [])
These return normal match-data objects.
Example:
To match against a simple, flat list of strings use:
(define (rope->string rope1 start rope2 end)
(if (eq? rope1 rope2)
(substring (car rope1) start end)
(let loop ((rope (cdr rope1))
(res (list (substring (car rope1) start))))
(if (eq? rope rope2)
(string-concatenate-reverse ; from SRFI-13
(cons (substring (car rope) 0 end) res))
(loop (cdr rope) (cons (car rope) res))))))
(define rope-chunker
(make-irregex-chunker (lambda (x) (and (pair? (cdr x)) (cdr x)))
car
(lambda (x) 0)
(lambda (x) (string-length (car x)))
rope->string))
(irregex-search/chunked rope-chunker )
Here we are just using the default start, end and substring behaviors,
so the above chunker could simply be defined as:
(define rope-chunker
(make-irregex-chunker (lambda (x) (and (pair? (cdr x)) (cdr x))) car))
===== irregex-fold/chunked
(irregex-fold/chunked [ []])
Chunked version of {{irregex-fold}}.
==== Utilities
The following procedures are also available.
===== irregex-quote
(irregex-quote )
Returns a new string with any special regular expression characters
escaped, to match the original string literally in POSIX regular
expressions.
===== irregex-opt
(irregex-opt )
Returns an optimized SRE matching any of the literal strings
in the list, like Emacs' \q{regexp-opt}. Note this optimization
doesn't help when irregex is able to build a DFA.
===== sre->string
(sre->string )
Convert an SRE to a POSIX-style regular expression string, if
possible.
=== License
Copyright (c) 2005-2010 Alex Shinn
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
=== References
* R. Kelsey, W. Clinger, J. Rees (eds.): ''[[http://www.schemers.org/Documents/Standards/R5RS/|Revised^5 Report on the Algorithmic Language Scheme]]''
* Russ Cox: ''[[http://swtch.com/~rsc/regexp/|Implementing Regular Expressions]]''
* Russ Cox: ''[[http://compilers.iecc.com/comparch/article/07-10-026|Henry Spencer's Tcl Regex Library]]''
* Olin Shivers: ''[[http://www.scsh.net/docu/post/sre.html|Proposed SRE regular-expression notation]]''
* Olin Shivers: ''[[http://www.scsh.net/docu/html/man-Z-H-7.html|Pattern-matching strings with regular expressions]]''
* Shiro Kawai: ''[[http://practical-scheme.net/gauche/man/gauche-refe_49.html|Gauche Scheme - Regular Expressions]]''
* Damian Conway: ''[[http://www.perl.com/pub/a/2002/08/22/exegesis5.html|Perl6 Exegesis 5 - Regular Expressions]]''
* Philip Hazel: ''[[http://www.pcre.org/|PCRE - Perl Compatible Regular Expressions]]''