Jump to content

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


Recommended Posts

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

  • Moderators

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

Link to comment
Share on other sites

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

Link to comment
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)
Link to comment
Share on other sites

?

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
Link to comment
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

Link to comment
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
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...