Page 1 of 1
Matching the dollar sign in a regular expression?
Posted: Sun Aug 19, 2007 3:11 pm
by cormullion
Is there some trick to searching for a plain unadorned dollar sign occurring in text with a regular expression? I'm trying to replace the two characters \$ when followed by one or two digits with a single character $ followed by the digit(s) found. I've lost sight of the answer in a downpour of baffling backslashes ... :-)
Posted: Sun Aug 19, 2007 3:28 pm
by Lutz
like this:
Code: Select all
(replace {\\\$} {abc\$cde} "x" 0) => "abcxcde"
(replace "\\\\\\$" "abc\\$cde" "x" 0) => "abcxcde"
- both the backslash \ and the dollar sign are special characters in regular expression patterns (not the string, only the pattern) and have to be escaped by a \
- additionally when using quotes "" than a backslash is also a special character for newLISP.\ and must be esacped by a \.
- the {,} in newLISP takes all chracters literally, makeing things a bit easier.
Lutz
Posted: Sun Aug 19, 2007 3:31 pm
by Lutz
... so in my examples I am actually replacing two character: the backslash and a dollar sign with an 'x'. Nor sure if you meant, the dollar sign only:
Code: Select all
(replace {\$} {abc$cde} "x" 0) => "abcxcde"
(replace "\\\$" "abc$cde" "x" 0) => "abcxcde"
Lutz