Sign in to follow this  
Followers 0
Bert

Regex - 7 digit number

14 posts in this topic

#1 ·  Posted (edited)

I'm trying to use Regex to verify a number.

Criteria:

  • number must be 7 digits long. 6 or under or 8 and more will cause an error.
  • must support leading zeros - example 0000023
  • no other characters permitted.

Examples of what will pass:

1234567

0004567

0234567

1234500

0022300

Examples of what will fail:

000001

0000000

12345

12 3456

001230

wedr000

I've been trying different things now for about an hour without any success. I did find this site that shows a bunch of examples:

http://regexlib.com/Search.aspx?k=number&c=3&m=-1&ps=100

Edited by Volly

Share this post


Link to post
Share on other sites



Dim $grantMe[5] = ["1234567","0004567","0234567","1234500","0022300"]
Dim $failMe[5] = ["000001", "0000000", "12345", "001230", "wedr000"]

ConsoleWrite("These must all win: " & @CRLF)
For $i = 0 to 4
    ConsoleWrite($grantMe[$i] & " - ")
    if (_test($grantMe[$i])) Then
        ConsoleWrite("win" & @CRLF)
    Else
        ConsoleWrite("fail" & @CRLF)
    EndIf
Next

ConsoleWrite("These must all fail: " & @CRLF)

For $i = 0 to 4
    ConsoleWrite($failMe[$i] & " - ")
    if (_test($failMe[$i])) Then
        ConsoleWrite("win" & @CRLF)
    Else
        ConsoleWrite("fail" & @CRLF)
    EndIf
Next

Func _test($s)
    return StringRegExp($s, "[0-9]{7}") And Number($s) <> 0
EndFunc

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I think I got it, but can someone test it to make sure I got it right? I've checked it a bunch and it seems to perform as expected.

^(000000[1-9])$|^(00000[1-9][0-9])$|^(0000[1-9][0-9][0-9])$|^(000[1-9][0-9][0-9][0-9])$|^(00[1-9][0-9][0-9][0-9][0-9])$|^(0[1-9][0-9][0-9][0-9][0-9][0-9])$|^([1-9][0-9][0-9][0-9][0-9][0-9][0-9])$
Edited by Volly

Share this post


Link to post
Share on other sites

I think I got it, but can someone test it to make sure I got it right? I've checked it a bunch and it seems to perform as expected.

^(000000[1-9])$|^(00000[1-9][0-9])$|^(0000[1-9][0-9][0-9])$|^(000[1-9][0-9][0-9][0-9])$|^(00[1-9][0-9][0-9][0-9][0-9])$|^(0[1-9][0-9][0-9][0-9][0-9][0-9])$|^([1-9][0-9][0-9][0-9][0-9][0-9][0-9])$

That's returning an error for me. Are you returning this in an array?

If you are then it's probably easier to just get any line containing 7 digits and when you loop through the array eliminate 0000000.

\d{7}


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

Yours is overcomplicated. Why dissect that much since you explicitely allow for leading zeroes?

Use {n} for fixed repetition. So the base would be "\A\d{7}\z"

But since you don't want an all-zeroes field, we have to exclude it, e.g. with a negative (we don't want) back (something already scanned) assertion (7 zeroes in a row)

Which leads us to this:

Local $string[11] = [ _
    "00000000000", _
    "0000000", _
    "00000", _
    "00abcd0", _
    "abc0000", _
    "0000def", _
    "1000000", _
    "0000001", _
    "0023400", _
    "002536400", _
    "123" _
]
For $str In $string
    $match = StringRegExp($str, "\A\d{7}(?<!0{7})\z")
    Consolewrite($match & " " & $str & @CRLF)
Next

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Manadar's code seems correct to me.

Share this post


Link to post
Share on other sites

That's true but doing it this way needs an extra conversion and test to check for '0000000'. Including the test within the regexp is easier as the engine is exactly in a position to do so without extra work and without backtracking since the matched zone (PCRE has a pointer to it and knows its current length, 7) is the same length as the excluded condition.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

I'm having to use this in javascript.

Are Javascript PCRE-based? Does the back-assertion work?

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Also give the Regexp Coach a try!


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

That's returning an error for me. Are you returning this in an array?

If you are then it's probably easier to just get any line containing 7 digits and when you loop through the array eliminate 0000000.

\d{7}

Tried yours, and it doesn't like the leading zeros.

Share this post


Link to post
Share on other sites

I always use Rubular. Pretty darn awesome site.


Broken link? PM me and I'll send you the file!

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