Change, because today I learned something new.
At the same time, I ran into the same problem as the question, and made the wrong conclusion that TRegEx does not perform TRegEx expansion at all .
The correct conclusion should be that TRegEx does not perform C-style reverse slash expansion in replacement string parameters, and I should investigate if it does string parameters in pattern .
I knew support for character exit mechanisms depends on the development tool .
For example, C, C #, Java, Perl, PHP, Ruby, bash and many others do the reverse flush. But since the Delphi compiler (since it is not a C-style compiler), no.
It will extend the Pascal-style escape style (e.g. #13#10 or ^M^J ) in CRLF.
So, I did this research today (thanks to David for pointing me to my original mistake), and came up with two examples ( one in Delphi and one in C # ) that has a function that basically does this:
- show the result of matching the known CRLF string pattern and the pattern containing the string
- show space replacement with string
Then the sample function is called:
- a string that is the backslash of the escape string \ r \ n in the source code, so it can be parsed by the compiler
- the string that the character composes, so it becomes the backslash of the escape string \ r \ n of the string so that it can be parsed using the RegEx mechanism
From the output in both examples, you will see that:
- Delphi compiler does not parse string \ r \ n
- C # compiler parses string \ r \ n
- The RegEx engine in Delphi and C # parses the \ r \ n pattern string at runtime (
RegEx documentation ) - The RegEx mechanism in both Delphi and C # does not parse the replace \ r \ n string at run time (
RegEx documentation )
The stil recommendation is:
So either use Pascal-style escape sequences, or use the C-style backslash extension function , for example Cosmin wrote.
As a side note: when using any extension function, you should keep in mind that this will change the meaning of the text. Delphi users may not expect C-style string expansion
Jeroen Wiert Pluimers
source share