previous up next     contents index
Next: Exceptions Up: Programming Constructs Previous: Weak Boxes

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 (of type  <regexp>) for repeated matches.

 

  figure1208


Figure 2.2: Grammar for regular expressions

The format of a regular expression is specified by the grammar in Figure 2.2. 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-345-b") ; => ((1 . 9) (5 . 9)) 
   (regexp-match "x+" "12345") ; => #f 
   (regexp-replace "me" "me casa" "su") ; => "su casa" 
   (define r2 (regexp "([Mm])e ([a-zA-Z]*)")) 
   (define insert " tex2html_wrap_inline6852 1y  tex2html_wrap_inline6852 2") 
   (regexp-replace r2 "Me Casa" insert) ; => "My Casa" 
   (regexp-replace r2 "me cerveza" insert) ; => "my cerveza" 

previous up next     contents index
Next: Exceptions Up: Programming Constructs Previous: Weak Boxes

PLT