Edit, as I learned something new today.
I ran in the same problem as the question a while ago, and took the wrong conclusion that
TRegEx
does not do any C-style backslash escape expansion at all.
The correct conclusion should have been that
TRegEx
does not do C-style backslash escape expansion in the replacement
string parameters, and I should research if it does in the pattern
string parameters.
I knew support of character escaping mechanisms varies by development tool.
For instance, C, C#, Java, Perl, PHP, Ruby, bash, and many more do backslash escape expansion.
But since the Delphi compiler (since it is not a C-style compiler) doesn't.
It will expand Pascal-style escapes (like #13#10
, or ^M^J
) into CRLF though.
So I did that research today (thanks David for pointing me to my initial mistake), and came up with two examples (one in Delphi and one in C#) that has a function that basically does this:
- show the pattern match result of a known CRLF string, and a pattern that contains a string
- show the replacement of space by a string
Then the example function is called by:
- a string that in source code is backslash escaped \r\n string, so might be parsed by the compiler
- a string that is put together character so it becomes a backslash escaped \r\n string runtime to it might get parsed by the RegEx engine
From the output in both examples, you see that:
- The Delphi compiler does not parse the \r\n string
- The C# compiler does parse the \r\n string
- The RegEx engine in both Delphi and C# parses the pattern \r\n string at run-time (
RegEx
documentation)
- The RegEx engine in both Delphi and C# do not parse the replace \r\n string at run-time (
RegEx
documentation)
The recommendation stil stands:
So either use the Pascal-style escapes, or use a C-Style backslash expansion function like Cosmin wrote.
As a side note: When using any expansion function, you should keep in mind that it will alter the meaning of text. Delphi users might not expect C-style expansion of strings.