[cl-ppcre-devel] Re: An idea for ppcre::register-groups-bind

Chris Dean ctdean at sokitomi.com
Mon Jun 12 23:40:38 UTC 2006


> "Alexander Kjeldaas" <alexander.kjeldaas at gmail.com> wrote:
>> The idiom is - you want to go through the text matching various
>> regexps, but you want to keep on to the end-position in order to do
>> incremental matching.  

I agree with Edi that the idea is basically sound, but finding a good
syntax will be challenging.  

In other languages (Perl mostly), I've used the same idiom for a quick
and dirty parser.  The difference is that I've always used a replace
on the target string instead of just a match.  The replacement text is
always the empty string "". That way, I remove what I match and can
continue on.

As an example, I'll invent REPLACE-REGISTER-GROUPS-BIND and use it to
parse the name, rank, and serial number out of a string.

    (defun parse-stuff (string)  
      (replace-register-groups-bind (name) ("Name: (\\S+)" string "")
       (process-name name))

      (replace-register-groups-bind (rank) ("Rank: (\\S+)" string "")
       (process-rank rank))

      (replace-register-groups-bind (sn) ("Serial Number: (\\S+)" string "")
       (process-sn sn)))

I probably should have copied the string first, but you get the idea.

If you want to go with a non-destructive solution I think the syntax
is tough.  The best I could come up with in the 30 seconds of
contemplation was the mythical REGISTER-GROUPS-BIND-2 form that binds
the start and end of the match:

    (defun parse-stuff (string)
      (let ((last-end 0))
        (register-groups-bind-2 (name) (match-start match-end) 
              (*name-re* string :start last-end)
          (process-name name)
          (setf last-end match-end))

        (register-groups-bind-2 (rank) (match-start match-end) 
              (*rank-re* string :start last-end)
          (process-rank rank)
          (setf last-end match-end))

        (register-groups-bind-2 (sn) (match-start match-end) 
              (*sn-re* string :start last-end)
          (process-sn sn)
          (setf last-end match-end))))

BTW, Perl has some anchoring meta characters (\G), but I don't think
that is what you are looking for.

Cheers,
Chris Dean




More information about the Cl-ppcre-devel mailing list