Thomymaster Posted February 25, 2015 Share Posted February 25, 2015 Hello I have some data which i have to test. The format is always the same here are some examples: 500 500-600 150-900 I have to check that the value should not contain any non-digits, BUT can contain the - I have If StringRegExp($sValue,"[\s\D]",0)=True Then MsgBox(0,"Test","data invalid") EndIf bit this throws the error when the - is in the string (obvious, as - is a non-digit) How can i modifiy the Regex so that the - can be in the text (only once) but no other non-digits? Cheers Thomy Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 25, 2015 Moderators Share Posted February 25, 2015 (edited) "^[d-]+$" Edit: Example: If StringRegExp($sValue, "^[\d-]+$") Then MsgBox(64, "Info", "Good Value") EndIf Edited February 25, 2015 by SmOke_N 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. Link to comment Share on other sites More sharing options...
kylomas Posted February 25, 2015 Share Posted February 25, 2015 Thomymaster, So "-9-10" is ? and "-22" is ? 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...
Thomymaster Posted February 25, 2015 Author Share Posted February 25, 2015 Thanks a lot Link to comment Share on other sites More sharing options...
kylomas Posted February 25, 2015 Share Posted February 25, 2015 (edited) This enforces 1 integer (+/-) folowed optionally by another integer (+/-)... local $sValue = '-100' if stringregexp($sValue,'^-?\d+(--?\d+)?$') then MsgBox(64,'Info', 'Good Value') Else MsgBox(64, 'ERROR', 'Invalid value: ' & $sValue) endif Edited February 25, 2015 by 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...
Radiance Posted February 25, 2015 Share Posted February 25, 2015 This weird RegExp voodoo amazes me everytime I see it. Link to comment Share on other sites More sharing options...
Malkey Posted February 25, 2015 Share Posted February 25, 2015 .... the - can be in the text (only once) .... Here is my guess about "-9-10" and "-22". In which MsgBox they should appear. Local $sGood, $sBad Local $sValue[7] = ["500", "500-600", "120A3", "-22", "150-90-60", "-9-10", ""] For $i = 0 To UBound($sValue) - 1 If StringRegExp($sValue[$i], "(^\d+-?\d*$)|(^-\d+$)") Then $sGood &= $sValue[$i] & @CRLF Else $sBad &= $sValue[$i] & @CRLF EndIf Next MsgBox(0, "Good Value", $sGood) MsgBox(0, "Bad Value", $sBad) Link to comment Share on other sites More sharing options...
kylomas Posted February 25, 2015 Share Posted February 25, 2015 @Malkey -9-10 (-9 to 10) might be a valid value...if I'm interpreting the OP's usage correctly... 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...
mikell Posted February 25, 2015 Share Posted February 25, 2015 (edited) ? Local $sValue[8] = ["500", "-500-600", "22-", "120A3", "-22", "150-90-60", "-9-10b", ""] For $i = 0 To UBound($sValue) - 1 If StringRegExp($sValue[$i], "^(-?\d+)*$") Then MsgBox(0, "Good Value", $sValue[$i]) Else MsgBox(0, "Bad Value", $sValue[$i]) EndIf Next Edited February 25, 2015 by mikell Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted February 25, 2015 Moderators Share Posted February 25, 2015 "^[d-]+(?<!-)$" 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. Link to comment Share on other sites More sharing options...
kylomas Posted February 25, 2015 Share Posted February 25, 2015 @mikell - that list this "150-90-60" as a good value violating this rule ".... the - can be in the text (only once) ....". The OP has not responded to my query about negative numbers so we may be splitting hairs... 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 February 25, 2015 Share Posted February 25, 2015 My idea : Local $sValue[8] = ["500", "-500-600", "22-", "120A3", "-22", "150-90-60", "-9-10b", ""] For $i = 0 To UBound($sValue) - 1 If StringRegExp($sValue[$i], "^\d+(-\d+)?$") Then MsgBox(0, "Good Value", $sValue[$i]) Else MsgBox(0, "Bad Value", $sValue[$i]) EndIf Next Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF Link to comment Share on other sites More sharing options...
mikell Posted February 25, 2015 Share Posted February 25, 2015 "^(-?\d+){0,2}$" this one allows negative numbers , but I agree requirements are not clear enough 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