Page 1 of 2

Replace text with regex (unexpected result)

Posted: 22.11.2021 17:16
by dc9f79b7
  1. Input text: 0 1 2 3 4 5 6 7 8 9
  2. Open Replace dialog (Ctrl+R) and enable regex mode (Alt+R)
    Search string: \d
    Replace string: \d
  3. Press "Replace all" (Alt+A)
  4. Result text: d d d d d d d d d d
  • If repeat with:
    Search string: \d
    Replace string: \\d
    then result text (that expected result): \d \d \d \d \d \d \d \d \d \d
How to fix this behavior?

• CudaText 1.149.3 x64
• Windows 7 SP1 x64

Posted: 22.11.2021 18:37
by main Alexey
The '\d' in the REPLACE field has no meaning! so editor reads it as "escaped d char" == "d".
The "\\d" is similar : "escaped slash + d".
What do you want to replace with what?
Maybe try to replace '\d' with '\\$0'.

Posted: 22.11.2021 19:10
by dc9f79b7
It is necessary to replace any digits in the text with "\d":
"0" to "\d"
...
"9" to "\d"

Why use escaping for "\d" or "\u" or "\w" (but not for "[" or "]" or "^" etc) in the replacement string? Is the "Replace" field also in regex mode?

Posted: 22.11.2021 19:11
by main Alexey
Somehow yes, it is-- some behaviour of the TRegExpr engine, which I may fix if its needed. How other good editors handle this case?

Posted: 22.11.2021 19:18
by main Alexey
SublimeText: acts like Cud now!
VSCode: not like Cud.
Not sure we need to change this.

Posted: 22.11.2021 19:22
by main Alexey
Supporting \ as escape is good - because we can set 'replace with' as '\n' and it will replace with EOLs. Same for '\t'.

Posted: 22.11.2021 19:27
by dc9f79b7
Ok. What about large fragments of code or markup? Each time you will have to first escape all the characters in the replacement string text.

Posted: 22.11.2021 19:42
by main Alexey
SublimeText users live with it somehow. maybe for large fragments, we should not use Regex.

Posted: 22.11.2021 19:47
by dc9f79b7
Yes "\n", "\t", "\r" etc. - is good. But mode work with "\q" and other characters.

Maybe take into account only: or make as option?

Posted: 22.11.2021 20:10
by main Alexey
Now it's logical- each \ means escaping, so literal \ must be written as \\.
If I do \ not working in \d and \q, it will be less logical. hard for user to know when he needs escaping.