Replace text with regex (unexpected result)

dc9f79b7
Posts: 9
Joined: 22.11.2021 13:14

Replace text with regex (unexpected result)

Post 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
main Alexey
Posts: 2708
Joined: 25.08.2021 18:15

Post 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'.
dc9f79b7
Posts: 9
Joined: 22.11.2021 13:14

Post 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?
main Alexey
Posts: 2708
Joined: 25.08.2021 18:15

Post 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?
main Alexey
Posts: 2708
Joined: 25.08.2021 18:15

Post by main Alexey »

SublimeText: acts like Cud now!
VSCode: not like Cud.
Not sure we need to change this.
main Alexey
Posts: 2708
Joined: 25.08.2021 18:15

Post 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'.
dc9f79b7
Posts: 9
Joined: 22.11.2021 13:14

Post 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.
main Alexey
Posts: 2708
Joined: 25.08.2021 18:15

Post by main Alexey »

SublimeText users live with it somehow. maybe for large fragments, we should not use Regex.
dc9f79b7
Posts: 9
Joined: 22.11.2021 13:14

Post 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?
main Alexey
Posts: 2708
Joined: 25.08.2021 18:15

Post 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.
Post Reply