Sign in to follow this  
Followers 0
kor

need help with expression checking

13 posts in this topic

#1 ·  Posted (edited)

I am by no means an expert when it comes to checking strings. I am wondering if someone can help me out. Here are my requirements.

The proper format is

3 digit site name, hyphen, room number

dom-345

brn-123

bal-553

orn-1103

brn-1409

etc

Site names can ONLY be the following

"dom", "bal", "crk", "grf", "orn", "brn"

If the site name is anything but those then error

the hyphen MUST be be present, and must be after the site name, and MUST be the 4th character

the first 2 characters after the hyphen MUST be numbers. If either of the 2 digits are letters (or any other character except a number) then error

Here is my code so far.

If $name <> "" Then
                    $name = StringLower($name)
                    $sitecode = StringSplit($name, "-")
                    Switch $sitecode[1] ; check to make sure that 3 digit site code is correct
                    Case "dom"
                        ; continue
                    Case "bal"
                        ; continue
                    Case "crk"
                        ; continue
                    Case "grf"
                        ; continue
                    Case "orn"
                        ; continue
                    Case "brn"
                        ; continue
                    Case Else
                        MsgBox (16, "Error", "Site code is incorrect")
                        GUICtrlSetState($input3, 256)
                    EndSwitch
                    If StringRegExp(StringLeft($sitecode[2], 2), "\D") < 1 Then ; check to make sure room number is only numbers
                        ExitLoop
                    Else
                        MsgBox (16, "Error", "Room cannot have letters")
                        GUICtrlSetState($input3, 256)
                    EndIf
            Else
                MsgBox(16, "Error", "Name cannot be blank")
            EndIf
Edited by kor

Share this post


Link to post
Share on other sites



I'm sure some RE's will pass by after some time.

You can also write those separate cases in one case statement. like

Case "dom", "bal", etc
- Switch string compare is case insensitive. All autoit operation are case insensitive, except for the "==" compare operation.

Func IsValidName($name)
    Return StringInStr('|dom|bal|crk|grf|orn|brn|', '|' & StringMid($name,1,3) & '|') <> 0 And StringMid($name,4,1)='~' And StringIsInt(StringMid($name,5,2))
EndFunc

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

I tried the Case "dom" Or "brn" Or statement but it wasn't working. I didn't know the proper format. Good to know for future reference it is just the different values and a comma.

Can you provide a little insight into what your function is doing step by step so I can learn?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Maybe would help you?! (It's in my signature!) It uses SRE!

Edited by guinness

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

I tried the Case "dom" Or "brn" Or statement but it wasn't working. I didn't know the proper format. Good to know for future reference it is just the different values and a comma.

Can you provide a little insight into what your function is doing step by step so I can learn?

The proper method is exactly as it was shown to you by the guy with the big long name that I'm not going to bother reading.

Case Dom, Bal, Crk, Grf, Orn, Brn
    ;Continue
Case Else
    ;Do something Else.
In Switch/Case statements a comma is the replacement for Or

And No! I'm not going to sit here and write the RegExp that will do it for you as that guy with the big long name suggested would happen.

There is a PCRE Toolkit in my signature that will get you started down that road.


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!"

Share this post


Link to post
Share on other sites

And No! I'm not going to sit here and write the RegExp that will do it for you as that guy with the big long name suggested would happen.

There is a PCRE Toolkit in my signature that will get you started down that road.

Pfft, took less time to write the pattern than it did for you to type the last sentence:
; Remove (?i) if you want it case sensitive
Global $gs_pattern = "(?i)^(?:dom|bal|crk|grf|orn|brn)-\d\d"
If Not StringRegExp($name, $gs_pattern) Then
    MsgBox(16, "Error", "Illegal name/pattern")
Else
    MsgBox(64, "Success", "Pattern is a match")
EndIf

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

....

the first 2 characters after the hyphen MUST be numbers. If either of the 2 digits are letters (or any other character except a number) then error

....

I half to three-quarters believe that the above means that after the hyphen there are only digits with a minimum of 2 digits, and no maximum number of digits.

In case one digit after the hyphen is allowed un-comment and comment the appropriate lines in the script.

Local $Test[10] = ["dom-345", "BRN-123", "Bal-553", "orn-11", "brn-1409", "dam-345", "dom345", "orn-1a", "brn-12b", "bal-5"]
Local $sRes
For $i = 0 To UBound($Test) - 1
    ; For any of these RE patterns (?i) causes case-insensitivity. Remove (?i) if default, case-sensitivity in required.

    ; This is Smoke_N pattern example here for testing.
    ;If StringRegExp($Test[$i], "(?i)^(?:dom|bal|crk|grf|orn|brn)-\d\d") Then

    ;If minimum of one digit after hyphen needed then use next line.
    ;If StringRegExp($Test[$i], "(?i)^(dom|bal|crk|grf|orn|brn)-\d+$") Then

    ;If minimum of two digits after hyphen needed then use this next line.
    If StringRegExp($Test[$i], "(?i)^(dom|bal|crk|grf|orn|brn)-\d{2,}$") Then
        $sRes &= $Test[$i] & @TAB & "ok" & @CRLF
    Else
        $sRes &= $Test[$i] & @TAB & "Fail" & @CRLF
    EndIf
Next
MsgBox(0, "Result", $sRes)

Share this post


Link to post
Share on other sites

@Malkey, I opted not to add a definite ending to the digits.

The reason for that was this quote from the op:

the first 2 characters after the hyphen MUST be numbers.

This implies that if there are more than 2 characters after the hyphen, they can be anything digit or otherwise.

The only rules the op specifically set forth was:

1. Must contain that set of 3 chars: dom|bal|crk|grf|orn|brn

2. Those 3 chars Must be followed by a hyphen

3. The next 2 characters must be digits.


Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites

Can you provide a little insight into what your function is doing step by step so I can learn?

Sure.

Note: Only syntax checked. Or ... not checked for logical errors.

Func IsValidName($name)
    ;; set default return state, bool.
    Local $fReturnState = True

    ;; check first 3 character of input string to (fullly) match one of the valid names.
    Local $sContoleList = '|dom|bal|crk|grf|orn|brn|'
    ;; Inner '|' are just data delimiters, the outer '|' are to make sure the search will also work on the first and last name.
    Local $sSearchString = '|' & StringMid($name, 1, 3) & '|'
    ;; - extrack first 3 character from the name string. And enclose them with '|' to make sure we only match up with full target names.
    If StringInStr($sContoleList, '|' & StringMid($name, 1, 3) & '|') <> 0 Then ;; or just '... > 0'
        ;; check was succuefull. continueing with next part checkup.

        ;; - extrack character 4 from name string, and compare to '~'
        If StringMid($name, 4, 1) = '~' Then
            ;; '~' match succesfull

            Local $sNumberPart = StringMid($name, 5, 2)
            ;; - extrack the the number part from the input string. (2 characters, starting from Position 5)
            ;; - check if the extracketed string only contains number characters. So no a.Z characters or '.' for that matter.
            If StringIsInt($sNumberPart) Then
                ;; 'match succesfull.
                ;; All matches where succesfull ... nothing more to do.

                ;; ... let code flow out of IF's, to the final return statement.
                ;; or ...
;~              Return $fReturnState
                ;; or ...
;~              Return False

            Else
                ;; number part failed.
                ;; - change return state to failed.
                $fReturnState = False
                ;; as there is no need to do any additional checking on other parts, we could jump out here.
;~              Return $fReturnState
                ;; or, if you don'e use a $fReturnState variable. just
;~              Return False
            EndIf
        Else
            ;; '~' match failed
            $fReturnState = False
        EndIf
    Else
        ;; first name part match failed
        $fReturnState = False
    EndIf
    
    Return $fReturnState
EndFunc

Whats up GEOSoft!. You had a bad nights rest or something. (nope, your right. I don't really care to know.)


"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Share this post


Link to post
Share on other sites

PMS


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!"

Share this post


Link to post
Share on other sites

I give up.The way I read the request seems to vary from other opinions . I don't generally do game code; which is what I think this is.

Here is my interpretation of what the OP wants to happen.

Local $aStr[7] = ["dom-345", "brn-123", "bal-553", "gbi-a578", "orn-1103", "brn-1409", "brn-18ab"]
$sExp = "(?i)^([dbocg][ora][mlkfn])-(\d{2}.*)$"

For $i = 0 To UBound($aStr) -1
    $aMatches = StringRegExp($aStr[$i], $sExp, 1)
    If NOT @Error Then
        MsgBox(4096, "Results", "Site Name; " & $aMatches[0] & @CRLF & "Room: " & $aMatches[1])
    Else
        MsgBox(4096, "Error", "Invalid Data Presented")
    EndIf
Next

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!"

Share this post


Link to post
Share on other sites

I thought I posted this once but it's not appearing now.

This one will be better since it will allow you to take different error dependent actions.

Opt('TrayIconDebug', 1)
Local $aStr[9] = ["dom-345", "brn-123", "bal-553", "gbi-a578", "orn-1103", "brn-1409", "brn-18ab", "dom-a356", "bal553"]
$sExp = "(?i)^([dbocg][ora][mlkfn])-(\d{2}.*)$"

For $i = 0 To UBound($aStr) - 1
    $sReturn = ""
    $aMatches = StringRegExp($aStr[$i], $sExp, 1)
    If NOT @Error Then
        MsgBox(4096, "Results", "Site Name; " & $aMatches[0] & @CRLF & "Room: " & $aMatches[1], 2)
    Else
        If NOT StringInStr($aStr[$i], "-") Then
            $sReturn &= "Hyphen was ommitted."
        Else
            If NOT StringRegExp($aStr[$i], ".*-(\d{2}.*)$") Then $sReturn &= "Room data is incorrect." & @CRLF
            If NOT StringRegExp($aStr[$i], "(?i)^([dbocg][ora][mlkfn])-.*") Then $sReturn &= "Site data is incorrect." & @CRLF
        EndIf
        MsgBox(4096, "Error", "Invalid Data Presented" & @CRLF & StringStripWS($sReturn, 2), 3)
    EndIf
Next

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!"

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0