1

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?)

Scott Chamberlain
  • 124,994
  • 33
  • 282
  • 431

3 Answers3

1

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.

Community
  • 1
  • 1
Ken Wayne VanderLinde
  • 18,915
  • 3
  • 47
  • 72
1

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.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
0

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

Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • Yes, but [^\t\r\b] would match all characters except those 3, I only want it to match characters under the value 32 expect those 3. – Scott Chamberlain Jun 27 '11 at 21:35
  • @Scott, you need to use that in conjunction with your other expression. I feel though that you might be looking at a negative lookbehind solution here. Can you post your sample text so that I can try lil more? – Mrchief Jun 27 '11 at 21:38
  • Please look at the if statement in the first code block, it shows the exact rules you need to follow, however I already got the answer, it was `(?![\t\r\n])[\x00-\x1F]` using negative lookaheads – Scott Chamberlain Jun 27 '11 at 21:40
  • @Scott, yeah I noticed and that's the negative lookbehind based pattern. – Mrchief Jun 27 '11 at 21:41