Sign in to follow this  
Followers 0
Thomymaster

Help on regex (how to exclude a character from "non-digits")?

13 posts in this topic

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

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

"^[d-]+$"

Edit:

Example:

If StringRegExp($sValue, "^[\d-]+$") Then
    MsgBox(64, "Info", "Good Value")
EndIf
Edited 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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Thanks a lot :)

Share this post


Link to post
Share on other sites

#5 ·  Posted (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 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

Share this post


Link to post
Share on other sites

This weird RegExp voodoo amazes me everytime I see it.

Share this post


Link to post
Share on other sites

.... 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)

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

#9 ·  Posted (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 by mikell

Share this post


Link to post
Share on other sites

"^[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.

Share this post


Link to post
Share on other sites

@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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

"^(-?\d+){0,2}$"

this one allows negative numbers , but I agree requirements are not clear enough

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