tags:

views:

43

answers:

3

Hi,

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.

+1  A: 

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.

skyfoot
.? will match 0-1 length string.
anand
There are so many possibilities: such as "[a_z]", or "\P{groupName}"
Peter Lee
@anand oh yeah, edited my post.
skyfoot
A: 

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.

Stefan Steinegger
Thanks for your reply. For English-only characters, your solution might be okay. but I'm processing Asian/Chinese text with a bunch of symbols, English/Japanese letters and so on.
Peter Lee
you may easily extend the syntax with this characters.
Stefan Steinegger
It it gets too complicated, try to apply the regex to single characters, as proposed in the first paragraph.
Stefan Steinegger
A: 

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.

Colin Hebert
Thanks for your reply. but I didn't get it. I'm not talking about the length of the regular expression itself, instead, I'm talking about the length of the final result strings should always be one.
Peter Lee
what about plain text? eg `ABC` would match a three-character string, while [ABC] would be valid.
Stefan Steinegger
@Stefan Steinegger, Exactly, that's why he'll need to count the number of characters ;)
Colin Hebert
but ... `[ABC]` has the same number of characters as `xj\w4`
Stefan Steinegger
@Stefan Steinegger that's why I also said "don't forget to flatten the `[]` as one character"
Colin Hebert