Click here to Skip to main content
15,500,977 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The attached code removes "control codes" from string.
Good.
However it also removes wanted / needed "new line" \n.

I need to keep the "new line" AKA \n .

How do I modify the regular expression to keep the new line characters?
Or what is the "control code" for new line?
result = inString.remove(QRegularExpression ("[\\000-\\037]+"));


What I have tried:

Looked for definition of "\n" as a control code ....

Newline - Wikipedia[^]

Are these in hex? \\000-\\037
Does this range includes "\n"? (since it is deleted it must be there...)
Posted
Updated 6-Jul-22 9:17am
v2

1 solution

\000 in a RegEx are ASCII values for characters in Octal (digits are 0 through 7)
\x00 are ASCII values in hexadecimal
For Unicode values, it's \u0000

Since you defined a range of values, you also included the values for Carriage Return and Line Feed, 13 and 10.

To keep those values, you need to remove them from the range: [\x00-0x31-[\x0A\x0D]]

Keep in mind, my RegEx is rusty and this is untested.

Google for "Expresso 3" and download a copy of it. It is indispensable when writing and testing RegEx expressions.
 
Share this answer
 
v2
Comments
merano99 6-Jul-22 14:23pm     CRLF
Nice, +5! With \x31 there will be a lot lost, Special characters that you do not want in texts end with \x1F (or \037)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900