Regular expression for max string length
I'm working on regular expression to prevent someone from typing too many characters into a Display Form textbox as well as using any characters that might confuse my feature class (apostrophe, quotation mark, etc.).
So far I've gotten this to work okay:
^[a-zA-Z\d\s,.?!-]{1,100}$
Does anyone know of a shorter or smoother way to set and test a max string length?
0
-
Using regex is probably the way to go on this one, I can't imagine a shorter or smoother way to do this.
You could use vb to count the characters in the string first with str.length() and then check that each character is in a whitelist like "a,b,c,d,e,f,g...1,2,3,4....?,!,". Use logic such as "WHILE boolean true: FOR char in str: IF list.Contains(char.toString) = false: ALERT usermessage, ASSIGN boolean=false; SWITCH boolean is false => DISPLAYFORM;". I would not do this, because its messier and slower than the alternative.
You should stick with the syntax that you're already using. Conventional wisdom is that reges is the way to go here. The only characters which are going to make it past this regex are alphabeticals and a comma, dot, questionmark, exclamation point, and hyphen. Perhaps add "0-9" in there with any more characters that you like. Some would add an underscore, depending on your ESRI environment.0 -
Thanks for the feedback. I was able to shorten the expression a little:
^[\w\s,.?!-]{1,100}$
\w is equivalent to [A-Za-z0-9_]
I found a good cheat sheet (http://www.mikesdotnetting.com/article/46/c-regular-expressions-cheat-sheet) .0 -
John thanks for this feed and cheat sheet.
Another example for others:
Need 17 exact digits, only numbers and dashes
35-017-20023-0000
^[\d-]{17}$0
Please sign in to leave a comment.
Comments
3 comments