Sign in to follow this  
Followers 0
the123punch

Regular expression for enumerating numbers

9 posts in this topic

Hi all,

Excuse my newbiness with regular expressions since I have never used them with AutoIt.

I would like to validate some user input using a regular expression. The user is supposed to input a string enumerating numbers (ex.: 1,5,78,46,35,7)

There is no limit to the amount of numbers they input, but they have to be separated by commas. Also, we should accept spaces after commas. The last number should not be followed by a comma.

Also, we should accept it if a user input only 1 number (ex.: 1)

This is what I tried using but it is not working, and it is probably totally due to my newbiness with Regular expressions in AutoIt.

If (IsInt ($txtTemp2) = False And StringRegExp($txtTemp2,"[\d+,]+[\d+]") =0) Then
    MsgBox(0, "Columns not in correct format", "Please enumerate column(s) in between commas! ex.(1,3,5)")
End If

I would appreciate any help!

the123punch

Share this post


Link to post
Share on other sites



you should use parenthesis instead of brackets for groupping a number with a comma.

If Not StringRegExp($szEntry, "\d+(, *\d+)*") Then

; error ...

Share this post


Link to post
Share on other sites

you should use parenthesis instead of brackets for groupping a number with a comma.

If Not StringRegExp($szEntry, "\d+(, *\d+)*") Then

; error ...

To add, I think you can probably also add the $ sign at the end of the RegEx, that is: "\d+(, *\d+)*$" to conform to your rule that the string can't end with a comma.

Share this post


Link to post
Share on other sites

To add, I think you can probably also add the $ sign at the end of the RegEx, that is: "\d+(, *\d+)*$" to conform to your rule that the string can't end with a comma.

Thanks for your quick answer.

I tried your regular expression and I think that we are almost there. However, I tried many strings to test it and it wrongly accepted the following string: "1,2,3f,5"

What can possibly cause that?

the123punch

Share this post


Link to post
Share on other sites

Here's my attempt. I'm not an expert in Regexp, but it seems to be working. I'm not sure if it is the best solution.

Local $input = InputBox("Input Numbers", "")
If StringRegExp($input, "\A\d+(, ?\d+)*\z") Then MsgBox(0, "Match", "TRUE")

Hi czardas,

I tried your regular expression and it seemed to work well on all stress tests that I gave it. Even the previous one which did not work with the other regular expression ("1,2,3f,5").

It solves my problem! Thanks a lot!

the123punch

Share this post


Link to post
Share on other sites

I'm glad it's what you want. BTW, it will only allow a single space after a comma.

Share this post


Link to post
Share on other sites

I'm glad it's what you want. BTW, it will only allow a single space after a comma.

Yes I know, and I modified it to accept multiple spaces after a comma.

Thanks a bunch.

the123punch

Share this post


Link to post
Share on other sites

Thanks for your quick answer.

I tried your regular expression and I think that we are almost there. However, I tried many strings to test it and it wrongly accepted the following string: "1,2,3f,5"

What can possibly cause that?

the123punch

I think it's because it wasn't actually matching the entire string, it was only matching the 5. Czardas fixed that by the "\A" at the start of the RegEx.

Demo below... hope it makes sense

#include <Array.au3>

$string = "1,2,3f,5"
$array = StringRegExp($string, "\A\d+(?:, ?\d+)*\Z", 3)
_ArrayDisplay($array)   ;Doesn't do anything because Array is invalid
ConsoleWrite($array)    ;Outputs 1 because there are no matches

$string = "1,2,3,5"
$array = StringRegExp($string, "\A\d+(?:, ?\d+)*\Z", 3)
_ArrayDisplay($array)   ;Displays $array[0] which is "1,2,3,5"
ConsoleWrite($array)    ;Outputs nothing because $array is an array

$string = "1,2,3f,5"
$array = StringRegExp($string, "\d+(?:, ?\d+)*\Z", 3)
_ArrayDisplay($array)   ;Displays $array[0] which is 5 because we didn't tell the RegEx to match at the beginning of the line
ConsoleWrite($array)    ;Outputs nothing because $array is an array

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