Summary

string-substitute \NUM syntax with preceding backslashes

Metadata

Description

I'm seeing an odd behavior with string-substitute. I want to prefix commas in a string with backslashes, but found the following behavior:

 > (use regex)
 
 > (string-substitute #\, "\\ \\0" "a,b")
 "a\\ ,b"
 
 > (string-substitute #\, "\\\\0" "a,b")
 "a\\\\0b"

The \NUM syntax in the substitution string worked as expected when preceded by a space, but not when preceded by another backslash (which was escaped).

CHICKEN version 4.9.0.1 regex version 1.0

Changes and comments

[2015-08-27 09:12:30 UTC] sjamaan changed milestone from someday to 4.11.0

[2015-08-27 09:12:30 UTC] sjamaan wrote:

This is an interesting bug, I would like to see what's causing this for 4.11

[2015-09-06 16:52:50 UTC] sjamaan changed status from new to closed

[2015-09-06 16:52:50 UTC] sjamaan set resolution to invalid

[2015-09-06 16:52:50 UTC] sjamaan wrote:

This seems to be an undocumented feature of the substring-replace function, which allows you to escape the backslash. I would recommend using irregex, the regex egg's API is kind of deprecated anyway, and it's also not very efficient.

Here's how to do this using irregex:

 #;1> (use irregex)
 #;2> (irregex-replace #\, "a,b" "\\" 0)
 "a\\,b"

You can also use irregex-replace/all if you want to replace more than just the first occurance.