newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on January 31, 2006, 06:48:13 AM

Title: What do $0 $1 $2 do in replace?
Post by: cormullion on January 31, 2006, 06:48:13 AM
Quotereplace with regular expressions also sets the internal variables $0 $1 $2 ... etc. with the contents of the expressions and sub-expressions found.


That's what the manual says. But what do $0 $1 etc really get set to? $0 is the entire matched pattern? Does it depend on whether you use parentheses in the regex pattern?
Title:
Post by: Lutz on January 31, 2006, 07:13:46 AM
Yes, $0 is for the whole pattern and $1, $2 etc. for the subpatterns indicated by (,) parens. Here is a quick example:



> (find {(http://)(new.*.)(org):(dd)} "http://newlisp.org:80" 0)
0
> $0
"http://newlisp.org:80"
> $1
"http://"
> $2
"newlisp."
> $3
"org"
> $4
"80"
>


Lutz
Title:
Post by: cormullion on January 31, 2006, 08:27:14 AM
I see - the $0 is then the equivalent of '&' and $1 the equivalent of '1' in other regex implementations...