开发者

Translate this if statement in to regex

开发者 https://www.devze.com 2023-03-16 05:09 出处:网络
I had to translate the following if statement in to a regex so we could use it on a whole string if (buffer[i] < 32 && !(buffer[i] == \'\\t\' || buffer[i] == \'\\r\' || buffer[i] == \'\\n\

I had to translate the following if statement in to a regex so we could use it on a whole string

if (buffer[i] < 32 && !(buffer[i] == '\t' || buffer[i] == '\r' || buffer[i] == '\n'))
    buffer[i] = ' ';

Which I did by doing this

return Regex.Replace(base.ReadLine(), "[\0-\x08\x0B\x0C\x0E-\x1F]", " ");

However I don't like the way it looks, is there a way in regex to do the same thing I did in 开发者_运维技巧the if statement? Basicly

[\0-\x1f]~[\t\r\b]

Where the ~ would go the thing that represents "exclude the following" (It does not have to be this exact syntax, I was just wondering if there is something like this?)


Take a look here - it talks about regex negations. I'm not sure if it can directly help you, but it's the best I've got.


If you don't like the look of your regex, you could use something like /(?![\t\r\n])[\x00-\x1F]/. The first part, (?![\t\r\n]), says to fail if the next character matches [\t\r\n], and then the second part [\x00-\x1F] says to match any character <= 0x1F.


To negate something, use [^\t\r\b]. You can also try negative lookbehinds but the former is simpler IMHO.

0

精彩评论

暂无评论...
验证码 换一张
取 消