iamtheky Posted October 12, 2016 Share Posted October 12, 2016 (edited) there will be many strings, of varying length and structure I want the computers that match the scheme of "XX-" followed by 4 letters and 3 numbers in any arrangement XX-EXCHG01 - does not match XX-GLG65J4 - matches XY-GLG65J4 - does not match XX-4JG5L6G - matches COMPUT3R - does not match heres the long way: msgbox(0, '' , _TestString("XX-Hel3l45")) msgbox(0, '' , _TestString("XY-Hel3l45")) msgbox(0, '' , _TestString("XX-Hel3l45678")) msgbox(0, '' , _TestString("XX-Hel3l45")) Func _TestString($sTest) $aTest = (stringleft($sTest , 3) = "XX-") ? stringsplit(stringtrimleft($sTest, 3) , "" , 2) : execute("return 0") $nCount = 0 $aCount = 0 If UBound($aTest) > 7 Then return 0 for $i = 0 to ubound($aTest) - 1 $nCount += StringIsDigit($aTest[$i]) $aCount += StringIsAlpha($aTest[$i]) If $i = ubound($aTest) - 1 AND $nCount = 3 AND $aCount = 4 Then return $sTest Next EndFunc Edited October 12, 2016 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
kylomas Posted October 12, 2016 Share Posted October 12, 2016 iamtheky, #include <array.au3> Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R' Local $aOriginal = StringSplit($str, ',', 3) For $1 = 0 To UBound($aOriginal) - 1 If _test($aOriginal[$1]) Then ConsoleWrite($aOriginal[$1] & ' matches' & @CRLF) Else ConsoleWrite($aOriginal[$1] & ' does not match' & @CRLF) EndIf Next Func _test($str) If StringLeft($str, 3) <> 'XX-' Then Return False $str = StringRegExpReplace($str, '(?i)xx-(.*)', '$1') $str = StringRegExpReplace($str, '\d', '', 3) $str = StringRegExpReplace($str, '[A-Za-z]', '', 4) If StringLen($str) = 0 Then Return True Else Return False EndIf EndFunc ;==>_test kylomas p.s. I'm sure the code can be optimized, just an example... Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
kylomas Posted October 12, 2016 Share Posted October 12, 2016 ooops, didn't see your edit.... Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
iamtheky Posted October 12, 2016 Author Share Posted October 12, 2016 oooh, but you have regexps, ive seen people with the power to combine those into magical one-liners. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
kylomas Posted October 12, 2016 Share Posted October 12, 2016 5 minutes ago, iamtheky said: ive seen people with the power to combine those into magical one-liners. Yea, but I'm not one of them...forgot about ternary construct... #include <array.au3> Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R' Local $aOriginal = StringSplit($str, ',', 3) For $1 = 0 To UBound($aOriginal) - 1 ConsoleWrite($aOriginal[$1] & (_test($aOriginal[$1]) ? ' matches' : ' does not match' ) & @CRLF) Next Func _test($str) If StringLeft($str, 3) <> 'XX-' Then Return False $str = StringRegExpReplace($str, '(?i)xx-(.*)', '$1') $str = StringRegExpReplace($str, '\d', '', 3) $str = StringRegExpReplace($str, '[A-Za-z]', '', 4) return ( stringlen($str = 0 ) ? true : false ) EndFunc ;==>_test kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
jguinch Posted October 12, 2016 Share Posted October 12, 2016 One-line regex : Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R' Local $aOriginal = StringSplit($str, ',', 3) For $i = 0 To UBound($aOriginal) - 1 ConsoleWrite($aOriginal[$i] & (_TestString($aOriginal[$i]) ? ' matches' : ' does not match' ) & @CRLF) Next Func _TestString($sTest) Return StringRegExp($sTest, "^XX-(?=(?:\d*[A-Z]){4})(?=(?:[A-Z]*\d){3})[A-Z0-9]{7}$") EndFunc iamtheky 1 Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
iamtheky Posted October 12, 2016 Author Share Posted October 12, 2016 So much to learn in that one line. Well done. ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Danyfirex Posted October 12, 2016 Share Posted October 12, 2016 I think you can do a simple version using StringRegExpReplace+@extended. Saludos Danysys.com AutoIt... UDFs: VirusTotal API 2.0 UDF - libZPlay UDF - Apps: Guitar Tab Tester - VirusTotal Hash Checker Examples: Text-to-Speech ISpVoice Interface - Get installed applications - Enable/Disable Network connection PrintHookProc - WINTRUST - Mute Microphone Level - Get Connected NetWorks - Create NetWork Connection ShortCut Link to comment Share on other sites More sharing options...
jguinch Posted October 12, 2016 Share Posted October 12, 2016 @Danyfirex : StringRegExpReplace + @extended was my first idea Local $str = 'XX-EXCHG01,XX-GLG65J4,XY-GLG65J4,XX-4JG5L6G,COMPUT3R' Local $aOriginal = StringSplit($str, ',', 3) For $i = 0 To UBound($aOriginal) - 1 ConsoleWrite($aOriginal[$i] & (_TestString($aOriginal[$i]) ? ' matches' : ' does not match' ) & @CRLF) Next Func _TestString($sTest) Local $s = StringRegExpReplace($sTest, "^XX-", "") If Not @extended Then Return 0 $s = StringRegExpReplace($s, "\d", "") If @extended <> 3 Then Return 0 $s = StringRegExpReplace($s, "[A-Z]", "") If @extended <> 4 Then Return 0 Return StringLen($s) = 0 EndFunc Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
kylomas Posted October 12, 2016 Share Posted October 12, 2016 Jguinch, Seems pretty close to what i posted. The one liner was sweet. Kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
jguinch Posted October 12, 2016 Share Posted October 12, 2016 Yes kylomas, pretty close, except that mine does not return True for a string like XX-ABCDEFGHI123456789 (I see you wrote too fast : stringlen($str = 0 ) => stringlen($str) = 0 ) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
kylomas Posted October 12, 2016 Share Posted October 12, 2016 17 minutes ago, jguinch said: Yes kylomas, pretty close, except that mine does not return True for a string like XX-ABCDEFGHI123456789 (I see you wrote too fast : stringlen($str = 0 ) => stringlen($str) = 0 ) Yes, corrected, thanks Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill 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