regex pattern for a range and above 127

杀马特。学长 韩版系。学妹 提交于 2020-01-03 15:59:12

问题


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:

  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.



来源:https://stackoverflow.com/questions/8336635/regex-pattern-for-a-range-and-above-127

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!