简体   繁体   中英

How to replace exact string using regex? In DrRacket

I've been working with some functions lately, and I have this one that it works for some cases, I need a general one. I need a regex such that it finds exactly what I'm looking for and not just traces of it. I have this, for example

>(regexp-replace* #rx"^123456$" "123456 FUBAR" "MATCH!")
"123456 FUBAR"

As you can see it's not replaceing '123456' with ''MATCH!

I tried using this:

> (regexp-replace* #rx"(?:^| )123456(?:$| )" "123456XXXXX FUBAR" "MATCH!")
"123456XXXXX FUBAR"

Which is EXACTLY what I DON'T need.

I've lurked thru some other posts but I think regexps in jScript and php work a little different since they use "/".

Thanks in advice!

Edit: Well. The use of the following #rx: ^a for example, does not work very well with strings like (a 0 (ab)) .

> (regexp-replace* "^(a)" "(a 0 (a b)) FUBAR" "MATCH!")
"(a 0 (a b)) FUBAR"

I mostly need it to work it that way. :P

Using word boundary ( \\b , should use pregexp syntax):

> (regexp-replace* #px"\\b123456\\b" "123456 FUBAR" "MATCH!")
"MATCH! FUBAR"
> (regexp-replace* #px"\\b123456\\b" "123456XXXXX FUBAR" "MATCH!")
"123456XXXXX FUBAR"
> (regexp-replace* #px"\\ba\\b" "(a 0 b) FUBAR" "MATCH!")
"(MATCH! 0 b) FUBAR"
> (regexp-replace* #px"a" "(a 0 (a b)) FUBAR" "MATCH!")
"(MATCH! 0 (MATCH! b)) FUBAR"

See Regexp Syntax

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM