[previous] [up] [next]     [index]
Next: System Utilities Up: PLT MzScheme: Language Manual Previous: Custodians

Regular Expressions

MzScheme provides built-in support for regular expression pattern matching on strings, implemented by Henry Spencer's package. Regular expressions are specified as strings, using the same pattern language as the Unix utility egrep. String-based regular expressions can be compiled into a regexp value for repeated matches. The internal size of a regexp value is limited to 32 kilobytes; this limit roughly corresponds to a source string with 32,000 literal characters or 5,000 special characters.

figure215888


Figure 10.1: Grammar for regular expressions

The format of a regular expression is specified by the grammar in Figure 10.1. A few subtle points about the regexp language are worth noting:

The regular expression procedures are:

Examples:

  (define r (regexp "(-[0-9]*)+")) 
  (regexp-match r "a-12-345b") ; => ("-12-345" "-345") 
  (regexp-match-positions r "a-12-345b") ; => ((1 . 10) (5 . 10)) 
  (regexp-match "x+" "12345") ; => #f 
  (regexp-replace "mi" "mi casa" "su") ; => "su casa" 
  (define r2 (regexp "([Mm])i ([a-zA-Z]*)")) 
  (define insert "\\1y \\2") 
  (regexp-replace r2 "Mi Casa" insert) ; => "My Casa" 
  (regexp-replace r2 "mi cerveza Mi Mi Mi" insert) ; => "my cerveza Mi Mi Mi"
  (regexp-replace* r2 "mi cerveza Mi Mi Mi" insert) ; => "my cerveza My Mi Mi" 

[previous] [up] [next]     [index]
Next: System Utilities Up: PLT MzScheme: Language Manual Previous: Custodians

PLT