user357086
user357086

Reputation: 404

regex pattern for a range and above 127

I need a regex such that it matches following plus anything ascii above 127 (i.e 7F hex and above). Below works fine for the given range.

string pattern = "[\x00-\x1F]";

Upvotes: 6

Views: 3406

Answers (2)

user166390
user166390

Reputation:

Either:

  1. Accept characters in both ranges (with an alternation, [a-b]|[x-z]), or;
  2. Use multiple ranges in a character group ([a-bx-z]), or;
  3. Negate the inverted range in the character group ([^c-w])
    • The negation includes a whole lot of things before c and after w, so it's not [necessarily] the same as the former two, but this can be used as an advantage.

The appropriate values of a, b, c, w, x, and z are left as a [trivial] exercise for the reader.

Happy coding.

Upvotes: 0

Vigrond
Vigrond

Reputation: 8198

Try the or operator | (pipe)

string pattern = "[\x00-\x1f]|[\x7f-\uffff]";

FF hex would be the max ASCII value.

Here's a cheat sheet for further reference: http://www.mikesdotnetting.com/Article/46/CSharp-Regular-Expressions-Cheat-Sheet

Upvotes: 3

Related Questions