Page 1 of 1
What do $0 $1 $2 do in replace?
Posted: Tue Jan 31, 2006 2:48 pm
by cormullion
replace 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?
Posted: Tue Jan 31, 2006 3:13 pm
by Lutz
Yes, $0 is for the whole pattern and $1, $2 etc. for the subpatterns indicated by (,) parens. Here is a quick example:
Code: Select all
> (find {(http://)(new.*\.)(org):(\d\d)} "http://newlisp.org:80" 0)
0
> $0
"http://newlisp.org:80"
> $1
"http://"
> $2
"newlisp."
> $3
"org"
> $4
"80"
>
Lutz
Posted: Tue Jan 31, 2006 4:27 pm
by cormullion
I see - the $0 is then the equivalent of '&' and $1 the equivalent of '\1' in other regex implementations...