Thomymaster Posted February 25, 2015 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
Moderators SmOke_N Posted February 25, 2015 Moderators 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.
kylomas Posted February 25, 2015 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
kylomas Posted February 25, 2015 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
Radiance Posted February 25, 2015 Posted February 25, 2015 This weird RegExp voodoo amazes me everytime I see it.
Malkey Posted February 25, 2015 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)
kylomas Posted February 25, 2015 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
mikell Posted February 25, 2015 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
Moderators SmOke_N Posted February 25, 2015 Moderators 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.
kylomas Posted February 25, 2015 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
jguinch Posted February 25, 2015 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
mikell Posted February 25, 2015 Posted February 25, 2015 "^(-?\d+){0,2}$" this one allows negative numbers , but I agree requirements are not clear enough
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