Wednesday 15 May 2013

.net - Regular expression to allow alphanumeric, max one space etc -


I am opening this thread which is exactly like any other but I can not understand any problem: I have a The input field that allows the alphanumeric string to be substituted with optional split space as a separator, then an alternate other alphanumeric string etc .... I am getting this regex:

  ^ ([0- 9a-zA-z] +?) * $   

This works! But the performance is very bad as soon as I have 2 hundred empty spaces and those 2 spaces are very far in the sentence. In the example given below, the result is fine in the second half if I put 2 places at the beginning of the sentence. But it lasts for 10 seconds or longer, then is located away.

Dzdff5464zdiophjazdioj ttttttttt zoddzdffdziophjazdioj ttttttttt zoddzdffdzdff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zoddzdfff Ttttt zo999 ddzdfff Ttttt zoddzdfff Ttttt zoddzdff

2 The spaces are after 999 . Do you have any ideas or suggestions for improving this reggae?

Thanks and relation

pf

ps: As soon as you enter, invalid characters in the string are not yet, especially 2 spaces.

Edit: Another example: 12345678901234567890 '==> 20 characters + 1 invalid character = & gt; The result is 5 valid four pairs of instant and it lasts 5 seconds to regex! I suggest changing the expression like this: (? I) ^ [0-9a-z] + (?: \ S [0- 9a-z] +) * $

 Enter the image details here

This is functionally similar, because it will match all alphanumeric characters, which Are only demarcated from the place. One major difference is that I took the initial word check to the expression, then the non-capture group (?: ... ) for the remaining space

non-capture group (?: ... ) are rapidly capturing group ( ... ) because the regex engine does not need to maintain the matched values. And on repeating words, the engine does not need to validate that by transferring the space \ s to the word group, the first character in the group is included in the square class.

You must have a typo in your character class [0- 9a-zA-z] last z , perhaps the upper case should be < There will be some strange unexpected results in the code> e-z format. In my expression, I initially compelled to add a code <(code) (? I) , in which case the case of insensitive mode was changed, and I changed the square class to [0 -9a-z] .

In my trial, I think your expression ^ ([0- 9a-z] +?) * $ takes about 0.03 seconds to process your sample text My recommended expression completes the same examination in approximately 0.000022 seconds with 2 extra locations towards the end. Wow, this is a wonderful delta.

No comments:

Post a Comment