How Can I Check If a C# Regular Expression Is Trying to Match 1-(and-only-1)-Character Strings?

StackOverflow https://stackoverflow.com/questions/3870344

  •  28-09-2019
  •  | 
  •  

Question

Maybe this is a very rare (or even dumb) question, but I do need it in my app.

How can I check if a C# regular expression is trying to match 1-character strings?

That means, I only allow the users to search 1-character strings. If the user is trying to search multi-character strings, an error message will be displaying to the users.

Did I make myself clear?

Thanks.

Peter

P.S.: I saw an answer about calculating the final matched strings' length, but for some unknown reason, the answer is gone. I thought it for a while, I think calculating the final matched strings length is okay, though it's gonna be kind of slow. Yet, the original question is very rare and tedious.

Was it helpful?

Solution

Instead of validating the regular expression, which could be complicated, you could apply it only on single characters instead of the whole string.

If this is not possible, you may want to limit the possibilities of regular expression to some certain features. For instance the user can only enter characters to match or characters to exclude. Then you build up the regex in your code.

eg:

  • ABC matches [ABC]
  • ^ABC matches [^ABC]
  • A-Z matches [A-Z]
  • # matches [0-9]
  • \w matches \w
  • AB#x-z matches [AB]|[0-9]|[x-z]|\w
  • which cases do you need to support?

This would be somewhat easy to parse and validate.

OTHER TIPS

a regexp would be .{1} This will allow any char though. if you only want alpanumeric then you can use [a-z0-9]{1} or shorthand /w{1}

Another option its to limit the number of chars a user can type in an input field. set a maxlength on it.

Yet another option is to save the forms input field to a char and not a string although you may need some handling around this to prevent errors.

Why not use maxlength and save to a char.

You can look for unescaped *, +, {}, ? etc. and count the number of characters (don't forget to flatten the [] as one character).

Basically you have to parse your regex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top