hallaplay835 Posted April 14, 2011 Share Posted April 14, 2011 (edited) Hi everyone, I am pretty new to AutoIt. I have opened a topic here to discuss string management. I have had a read through the help file under the 'String Management' directory but I still cannot find a way to do what I am going to describe below without using extensive code. What I want to do is the following. I want to compare the contents of a string to a set of rules. The string must contain: 1. Less than 32 characters. 2. Only alphanumeric values (e.g Aa-Zz, 0-9). Spaces are also allowed. 3. One of the characters MUST be a letter. 4. However, there are two exceptions to rule no. 2. The string CAN contain also dashes '-' and/or underscores '_'. Rule 1 is easy. I suppose you could use the StringLen() function. The rest of the rules I do not know how to account for them in my code. I suppose you could put all the rules in an If statement with And and Or operators, and everything should work. Then, in the Else section, you could throw an error. Any plausible ideas as to how to solve this problem? Thanks in advance. Edited April 14, 2011 by hallaplay835 _____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size] Link to comment Share on other sites More sharing options...
MrMitchell Posted April 14, 2011 Share Posted April 14, 2011 (edited) StringRegExp() should do it. GEOSoft has a nice tool to test it tooCheck post and the link to PCRE Tool Kit in his sig... Edited April 14, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 14, 2011 Moderators Share Posted April 14, 2011 (edited) hallaplay835, Here is my idea of how it might work: Global $aString[5] $aString[0] = "123456789 12345678901234567-_A" ; Should pass $aString[1] = "123456789012345678901234567-_0" ; Should fail - no A-Z $aString[2] = "123456789012345678901234567890123" ; Should fail - too long $aString[3] = "123456789 1234567890123456-_A" ; Should Pass $aString[4] = "ABCDEFGHIJKLMNOP?URSTUVWXYZ_-" ; Should fail - contains ? For $i = 0 To 4 ; Check length If StringLen($aString[$i]) > 31 Then ConsoleWrite("Error - String " & $i & " is too long" & @CRLF) Else ; Check only digits, a-z (case-insensitive), dash, underscore or space If StringRegExp($aString[$i], "(?i)[^0-9a-z-_\x20]") Then ConsoleWrite("Error - String " & $i & " contains an illegal character" & @CRLF) Else ; Check there is at least 1 a-z (case-insensitive) If Not StringRegExp($aString[$i], "(?i)[a-z]") Then ConsoleWrite("Error - String " & $i & " does not contain at least one A-Z character" & @CRLF) Else ConsoleWrite("Good - String " & $i & " is correct" & @CRLF) EndIf EndIf EndIf Next I hope that gives you a start. M23 Edit: Oh dear, George is looking at this thread - donning tin hat and awaiting incoming! Edited April 14, 2011 by Melba23  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area  Link to comment Share on other sites More sharing options...
GEOSoft Posted April 14, 2011 Share Posted April 14, 2011 $sString = "The rest of the rules I do not " _Match($sString) $sString = "The rest of the rules I do not know how to account for them in my code" _Match($sString) Func _Match($sStr) If StringRegExp($sStr, "^[[:alnum:]_-\h]{1,31}$") Then MsgBox(4096, "Result", "We have a match") Else MsgBox(4096, "Result", "We have a problem") EndIf EndFuncIf it's possible for the string to be a complete sentence where it could end in punctuation then I would change the expression to"^[[:alnum:]_-\h]{1,31}[.?!]$" George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
GEOSoft Posted April 14, 2011 Share Posted April 14, 2011 (edited) hallaplay835, Here is my idea of how it might work: <snipped> M23 Edit: Oh dear, George is looking at this thread - donning tin hat and awaiting incoming! You were close but you went the hard way. Classes are good in this situation and with the [:alnum:] class you don't have to set case insensitive. EDIT: I don't remember if I have been in touch since the last update but make sure you are using ver:3.0.1.9 of the toolkit. Edited April 14, 2011 by GEOSoft George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
jchd Posted April 14, 2011 Share Posted April 14, 2011 Hi George, You don't enforce rule #3. use a lookbehind assertion This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
GEOSoft Posted April 14, 2011 Share Posted April 14, 2011 Hi George, You don't enforce rule #3. use a lookbehind assertion Possible. I just ran a quick test at the time and it worked. What did I miss? George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
MrMitchell Posted April 14, 2011 Share Posted April 14, 2011 (edited) What did I miss?That the string must contain at least 1 letter Edited April 14, 2011 by MrMitchell Link to comment Share on other sites More sharing options...
GEOSoft Posted April 14, 2011 Share Posted April 14, 2011 That the string must contain at least 1 letterI didn't read the post that he needed it that way. Easy fix as soon as I get a chance to look again. George Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.*** The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number. Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else. "Old age and treachery will always overcome youth and skill!" Link to comment Share on other sites More sharing options...
hallaplay835 Posted April 15, 2011 Author Share Posted April 15, 2011 StringRegExp() should do it. GEOSoft has a nice tool to test it tooCheck post and the link to PCRE Tool Kit in his sig...Thank you MrMitchell. _____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size] Link to comment Share on other sites More sharing options...
hallaplay835 Posted April 15, 2011 Author Share Posted April 15, 2011 hallaplay835, Here is my idea of how it might work: Global $aString[5] $aString[0] = "123456789 12345678901234567-_A" ; Should pass $aString[1] = "123456789012345678901234567-_0" ; Should fail - no A-Z $aString[2] = "123456789012345678901234567890123" ; Should fail - too long $aString[3] = "123456789 1234567890123456-_A" ; Should Pass $aString[4] = "ABCDEFGHIJKLMNOP?URSTUVWXYZ_-" ; Should fail - contains ? For $i = 0 To 4 ; Check length If StringLen($aString[$i]) > 31 Then ConsoleWrite("Error - String " & $i & " is too long" & @CRLF) Else ; Check only digits, a-z (case-insensitive), dash, underscore or space If StringRegExp($aString[$i], "(?i)[^0-9a-z-_\x20]") Then ConsoleWrite("Error - String " & $i & " contains an illegal character" & @CRLF) Else ; Check there is at least 1 a-z (case-insensitive) If Not StringRegExp($aString[$i], "(?i)[a-z]") Then ConsoleWrite("Error - String " & $i & " does not contain at least one A-Z character" & @CRLF) Else ConsoleWrite("Good - String " & $i & " is correct" & @CRLF) EndIf EndIf EndIf Next I hope that gives you a start. M23 Edit: Oh dear, George is looking at this thread - donning tin hat and awaiting incoming! Thanks a lot Melba23, but I think I prefer GEO Soft's approach using classes. Less code. Anyway, thanks. _____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size] Link to comment Share on other sites More sharing options...
hallaplay835 Posted April 15, 2011 Author Share Posted April 15, 2011 $sString = "The rest of the rules I do not " _Match($sString) $sString = "The rest of the rules I do not know how to account for them in my code" _Match($sString) Func _Match($sStr) If StringRegExp($sStr, "^[[:alnum:]_-\h]{1,31}$") Then MsgBox(4096, "Result", "We have a match") Else MsgBox(4096, "Result", "We have a problem") EndIf EndFunc If it's possible for the string to be a complete sentence where it could end in punctuation then I would change the expression to "^[[:alnum:]_-\h]{1,31}[.?!]$" Thank you very much for your answer, GEO Soft. It is all what I expected. However, you do not account for rule 3: the string must contain at least one letter. I am working on that right now. I have also found interesting the toolkit in your sig. Thanks for everything. _____________________________________________________[size="2"][font="Arial"]"Pain is temporary, glory is forever."[/font][/size] Link to comment Share on other sites More sharing options...
jchd Posted April 15, 2011 Share Posted April 15, 2011 (edited) Use the pattern with a look-ahead: ^(?=[^[:alpha:]]*[[:alpha:]])[[:alnum:]_-\h]{1,31}$That assertion is "zero or more non-letters followed by a letter", i.e. "at least one letter". Asserts don't consume anything in the subject, just test the condition.Contrary to what I wrote in the spoiler few post ago: lookahead is simpler than lookbehind. A lookbehind assertion is restricted to fixed or rather bounded length.So ^[[:alnum:]_-\h]{1,31}(?=[^[:alpha:]]*[[:alpha:]])$ gives an error due to the * in the assert (unbounded length)But rewriten this way, it works: ^[[:alnum:]_-\h]{1,31}(?=[^[:alpha:]]{0,30}[[:alpha:]])$ Edited April 15, 2011 by jchd This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now