Zum Hauptinhalt gehen

Regular expression for max string length

Kommentare

3 Kommentare

  • Zack Robison
    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
  • John Nerge
    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
  • Permanently deleted user
    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

Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.