TIP #75 Version 1.1: Refer to Sub-RegExps Inside 'switch -regexp' Bodies

This is not necessarily the current version of this TIP.


TIP:75
Title:Refer to Sub-RegExps Inside 'switch -regexp' Bodies
Version:$Revision: 1.1 $
Author:János Holányi <csani at lme dot linux dot hu>
State:Draft
Type:Project
Tcl-Version:8.4
Vote:Pending
Created:Wednesday, 28 November 2001
Discussions To:http://purl.org/mini/cgi-bin/chat.cgi
Keywords:switch, regexp, parentheses

Abstract

Currently, it is necessary to match a regular expression against a string twice in order to get the sub-expressions out of the matched string. This TIP alters that so that those sub-exps can be substituted directly into the body of the script to be executed.

Rationale

Similarly to the

   regexp -- <RE> $string matchvar submatchvar ...

of Tcl and the

   interact -re <RE> {
      set matches "$interact_out(0,string) $interact_out(1,string) ..."
   }

of Tcl/Expect, it would be very helpful and would also make Tcl more consistent if the [switch] command of Tcl would support references to parenthesized REs inside the switch patterns from the bodies associated to each of the patterns. As it is, it is currently necessary to match the regular expression against the string twice to obtain this information.

Specification

Actually, no new syntax is needed to achieve the mentioned ability. The solution could adopt the behavior of [regsub] (description taken from regsub(n)):

If subSpec contains a `&' or `\0', then it is replaced in the substitution with the portion of string that matched exp. If subSpec contains a `\n', where n is a digit between 1 and 9, then it is replaced in the substitution with the portion of string that matched the n-th parenthesized subexpression of exp. Additional backslashes may be used in subSpec to prevent special interpretation of `&' or `\0' or `\n' or backslash.

Alternatives

The other solution would be mapping the regular expression into an array in the style of [expect] and [interact] commands of Tcl/Expect, but that solution would require more resources I believe...

Reference Implementation

Not yet...

Copyright

This document has been placed in the public domain.


Powered by TclThis is not necessarily the current version of this TIP.

TIP AutoGenerator - written by Donal K. Fellows