0

I have got list of ASCII Codes of character which are allowed in the name.So, I am trying to build regex patter using range(-) hypen operator and string length should be 40.

Finally, I have ended up by creating following pattern.

^([\x20-\x21\x23-\x25\x28-\x2E\x30-\x3B\x3F-\x7E\xA0-\xFF]){0,40}$

with this pattern I am specifying the range of character's ASCII codes allowed in the string.

But it throws exception of Range in reverse order. I have gone through couple of question. I have found that it happens because of hypen(-) character. So, In order to remove this problem I have to use escape sequence (-) instead of(-).

After adding escape sequence although it doesn't throw exception but it doesn't give the desire result.

So, I want to know is my pattern is correct or Is it right away to specify ASCII Code character range.

7
  • \x3F-7E -> \x3F-\x7E. Just a typo, right? Commented Nov 18, 2015 at 9:05
  • Yes it was typo, but If I correct it. Still it doesn't give desired result Commented Nov 18, 2015 at 9:09
  • 1
    What is the desired result? String must be exactly 40 symbols long? Remove 0,. And remove ( and ), no need grouping 1 symbol. Commented Nov 18, 2015 at 9:10
  • \x19 is not valid character so, it should not match. Regex.IsMatch() should return false Commented Nov 18, 2015 at 9:11
  • It does not match (note the \x19 before here). Commented Nov 18, 2015 at 9:13

1 Answer 1

3

You need to use a negated character class, remove the grouping, quantifier, anchors and fix the typo:

[^\x20-\x21\x23-\x25\x28-\x2E\x30-\x3B\x3F-\x7E\xA0-\xFF]

See the regex demo (1 match is found before here) and use it as shown in the below C# demo:

var str = "  some text here ";
if (str.Length > 40 || Regex.IsMatch(str, @"[^\x20-\x21\x23-\x25\x28-\x2E\x30-\x3B\x3F-\x7E\xA0-\xFF]"))
    Console.WriteLine("The line is too long or contains invalid char(s)!");

Note that a negated character class is formed with the help of [^....] notation and matches all characters other than those specified in the character class.

If performance is key, you need to declare the regex as a static readonly field with RegexOptions.Compiled flag. Have a look at the Kurt Schindler's Regular expression performance comparisons blog.

Sign up to request clarification or add additional context in comments.

6 Comments

Ya it works fine but I had to add check length of string should be between lower limit 0 and upper limit 40. So, I had to add {0,40}$ in the end of expression
Why do you need a regex for that? Just check the string length. It is possible to do with regex , but why should you sacrifice performance? Add str.Length > 40 ||.
Yes I agree with you that it would have performance hit but in my case I would be using the same expression so many places. So, Instead of using extra code, I wan to do with the help of regex. I have read that {0,40}$ it should work perfectly fine for me but with the conjuction of above pattern it is not working. Moreover, its going to be very small string. So, no risk of performance hits
Please check this demo showing that @"^[\x20-\x21\x23-\x25\x28-\x2E\x30-\x3B\x3F-\x7E\xA0-\xFF]{0,40}$" should be working for you.
I have found out that already in-fact this is the same what I had started
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.