What do $0 $1 $2 do in replace?

Started by cormullion, January 31, 2006, 06:48:13 AM

Previous topic - Next topic

cormullion

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?

Lutz

#1
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

cormullion

#2
I see - the $0 is then the equivalent of '&' and $1 the equivalent of '1' in other regex implementations...