Jump to content

Need help with RegEx


Recommended Posts

Hello,

I am stuck trying to come up with a RegEx that will filter a list of data.

The data is an array of folder names on a system, and I am trying to identify folder names that start with letters and end with 4 numbers.

The folders will always start with an amount of letters equal to 2 or greater. The name must also end with 4 numbers and have no more data after that.

 

For example here are some valid names I am trying to catch: aaaaa1234, aa4444, asd6543, aaaaaaaaaaaaaaaaaaaaaaaa3333 (you get the idea).

So far I have a way to get the 4 numbers on the right: 

StringRegExp($ProfileNames[$i], "\d{4}\Z", 0)

 

Thanks in advance!

 

Link to comment
Share on other sites

  • Moderators

Can you confirm your criteria as being: at least 2 letters to begin and no more than four numbers to end? Anything in the middle is irrelevant?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

18 minutes ago, JLogan3o13 said:

Can you confirm your criteria as being: at least 2 letters to begin and no more than four numbers to end? Anything in the middle is irrelevant?

That is correct! The only catch is that anything in the middle must not be a number.

Thanks!

 

Link to comment
Share on other sites

  • Moderators
1 minute ago, aseitz said:

The only catch is that anything in the middle must not be a number.

These kinds of caveats are good to know off the bat. So the file name must have at least 2 letters at the beginning, and only 4 numbers at the end?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Try this:

Local $aFolders = [ _
    "aaaaa1234", _                      ; OK
    "aa4444", _                         ; OK
    "asd6543", _                        ; OK
    "aaaaaaaaaaaaaaaaaaaaaaaa3333", _   ; OK
    "a45679", _                         ; no only 1 letter ahead)
    "aaaaa12", _                        ; no (no 4 digits trailing)
    "a2", _                             ; no (too short)
    "2", _                              ; no (too short)
    "a", _                              ; no (too short)
    "没有这样的文件1234", _              ; OK ("letters" ahead)
    "zzzzzzz45645", _                   ; no (digits in between [more than 4 trailing)
    "fgdshnio333fno4567", _             ; no (digits in between)
    "3fdsnofds*******4567", _           ; no (starts with digit)
    "Русский/////////۳۵٢٨" _         ; OK (letters ahead, 4 decimal digits trailing (Hindi digits)
]

For $s In $aFolders
    MsgBox(0, "", $s & @LF & (StringRegExp($s, "(*UCP)^\p{L}{2}\D*\d{4}$") ? " matches" : " fails") & " the criterion.")
Next

As you can see, Unicode letters and decimal digits are correctly recognized and considered, not only A-Z and 0-9. If you're sure to never have to deal with non-ASCII letters (and I mean a-z A-Z, excluding letters with diacritics), replace

(*UCP)^\p{L}
by
[[:alpha:]]

in the pattern.

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)

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