Regex to replace certain control characters, except for a few special cases of C #?

i has the following requirement:

I have a str string that has control characters ... I want to replace these control characters with some specific values. Therefore, I use the following Regex as:

str = Regex.Replace(str, @"\p{C}+","\r\n");

The above replaces ALL control characters with \ r \ n.

However, I want to do the same above, but exclude the following control characters:

SPACE , `\u000D`, `\u000A`

How can I modify RegEx above to accomplish this? Any ideas? thank!

+4
source share
2 answers

Use character class subtraction :

str = Regex.Replace(str, @"[\p{C}-[ \u000D\u000A]]+","\r\n");
                           ^^^^^^^^^^^^^^^^^^^^^^^

[\p{C}-[ \u000D\u000A]]+ 1 \p{C} Unicode, , \u000D \u000A.

+4

: [^\P{C}\r\n]+

[^
\P{C} ( + = \P{C})
\r
\n

: , CRLF.

(btw: SPACE \P{C})

+1

Source: https://habr.com/ru/post/1691710/


All Articles