开发者

Simplifying Regex's - escaping

I want to enable my users to specify the allowed characters in a given string.

So... Regex's are great but too tough for my users.

my plan is to enable users to specify a list of allowed characters - for example

a-z|A-Z|0-9|,

i can transform this into a rege开发者_JAVA技巧x which does the matching as such:

[a-zA-Z0-9,]*

However i'm a little lost to deal with all the escaping - imagine if a user specified

a-z|A-Z|0-9| |,|||\|*|[|]|{|}|(|)

Clearly one option is to deal with every case individually but before i write such a nasty solution - is there some nifty way to do this?

Thanks

David


Forget regex, here is a much simpler solution:

bool isInputValid = inputString.All(c => allowedChars.Contains(c));


You might be right about your customers, but you could provide some introductory regex material and see how they get on - you might be surprised.

If you really need to simplify, you'll probably need to jetison the use of pipe characters too, and provide an alternative such as putting each item on a new line (in a multi line text box for instance).


To make it as simple as possible for your users, why don't you ditch the "|" and the concept of character ranges, e.g., "a-z", and get them just to type the complete list of characters they want to allow:

abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 *{}()

You get the idea. I think this will be much simpler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜