s c h e m a t i c s : c o o k b o o k

/ PatternMatching? / Cookbook.RegexCommentRegExp

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Commenting Regular Expressions

Problem

You want to make it easy to maintain and understand your complex regular expressions.

Solution

Use comments inside the pattern with the x modifier.

Discussion

The pregexp.ss module provides the ability to intersperse comments in your regular expression definition using the Perl-compatible x option in the definition:

(require (lib "pregexp.ss" "mzlib")
         (lib "dns.ss" "net"))

(define commented-regexp
  (pregexp
    "((?x:             ; Ignore whitespace
       (?x:            ; Capture the inet address
        (?! [\\-_] )   ; don't lookahead for underscore/dash
        [\\w\\-] +     ; hostname component
        \\.            ; dot
       )+              ; repeat a few times
       [A-Za-z]        ; ... followed by a letter
       [\\w\\-] +      ; and trailing domain part
     ))"))

(define (make-detailed-name name)
  (let* ((addr (dns-get-address "ns1.granitecanyon.com" name))
        (replace (string-append "\\1 [" addr "]")))
    (pregexp-replace commented-regexp name replace)))

> (make-detailed-name "alpha.gnu.org")
"alpha.gnu.org [199.232.41.11]"
> 


Comments about this recipe

Contributors

-- BrentAFulgham - 18 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: RegexRecipes
TopicOrder:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest