Matching the dollar sign in a regular expression?

For the Compleat Fan
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Matching the dollar sign in a regular expression?

Post 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 ... :-)

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post 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

Locked