问题
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]";
回答1:
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
回答2:
Either:
- Accept characters in both ranges (with an alternation,
[a-b]|[x-z]
), or; - Use multiple ranges in a character group (
[a-bx-z]
), or; - Negate the inverted range in the character group (
[^c-w]
)- The negation includes a whole lot of things before
c
and afterw
, so it's not [necessarily] the same as the former two, but this can be used as an advantage.
- The negation includes a whole lot of things before
The appropriate values of a
, b
, c
, w
, x
, and z
are left as a [trivial] exercise for the reader.
Happy coding.
来源:https://stackoverflow.com/questions/8336635/regex-pattern-for-a-range-and-above-127