rcmaehl

Regex question

5 posts in this topic

Currently I have

Switch StringLen($Serial)
        Case 0
            Return "Please Enter a Serial #"
        Case 1 To 6
            Return "UNKNOWN or INCORRECT"
        Case 7
            Select
                Case StringLeft($Serial, 2) = "79"
                    $Valid = "Device 1"
                Case StringLeft($Serial, 3) = "350"
                    $Valid = "Device 2"
                Case StringRegExp($Serial, "72[HR](.*)") ; Match 72H and 72R
                    $Valid = "Device 3"
                Case Else
                    $Valid = "UNKNOWN or INCORRECT"
            EndSelect
        Case 8
            Select
                Case StringRegExp($Serial, "\d[A-Z](.*)") ; Match a Digit (0-9), then a letter
                    $Valid = "Device 4 short format"
                Case Else
                    $Valid = "UNKNOWN or INCORRECT"
            EndSelect
    EndSwitch

Which I'm using to match a number followed by a letter then any number of digits up to a string length of 8 characters.

However I don't want it to match strings such as 1234A123 only 1D123456

Is there anyway to do this using a flag or am I limited to having to use StringLeft()?


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

1 hour ago, rcmaehl said:

Which I'm using to match a number followed by a letter then any number of digits up to a string length of 8 characters.

However I don't want it to match strings such as 1234A123 only 1D123456

Looks like a contradiction here: 1D123456 matches the first sentence. 1234A123 would also match if one interprets "a number" literally, whereas I believe you meant "a digit".

What about "^\d[A-Z]\d{0,6}" assuming "any number" really means what it says.

Edited by jchd

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
2 minutes ago, jchd said:

Looks like a contradiction here: 1D123456 matches the first sentence. 1234A123 would also match if one interprets "a number" literally, whereas I believe you meant "a digit".

What about "^\d[A-Z]\d{0,6}$" assuming "any number" really means what it says.

That works too, I was just hoping that there was a simple way to do "If Entire String Matches Regex Return True, Otherwise Return False"


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

Share this post


Link to post
Share on other sites

I don't get it. The contradiction is still there.

You speak of case 8, correct?

Then what do you allow after the digit followed by the letter followed by any number of digits? Isn't that also contradictory with length = 8?


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

Found what I was looking for

StringRegExp($Serial, "\A\d[A-Z]\d{0,6}$")


Case 8 doesn't matter I just needed it to Return True if the above was met. I realize now that what I posted was confusing as I said any number of digits but the code contradicted me.


My UDFs are generally for me. If they aren't updated for a while, it means I'm not using them myself. As soon as I start using them again, they'll get updated.

MY PROJECTS


Active: IRC UDF, WindowEx UDF
Discontinued: GithubBubbleSort UDF

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

  • Similar Content

    • MuffinMan
      Title text match with spaces
      By MuffinMan
      I am trying to rebuild an old single signon script that has been giving us a bit of trouble lately.  I didn't write it and it was written in another script language.  If the target program is already open it will have the current user's last name as part of the window text, which I am trying to compare against the user name to see if I should just WinActivate or Process Close and start the program over.  I will be provided the username like "jones", or "smith" and the title check works fine for last names (with no spaces), but if the last name DOES contains spaces, say "Nom De Plume", then all I will be given as a username is "nomdeplume".  Is there any way (via a regex maybe) that I can still check the text on the screen and somehow ?  I have been reading about the Advanced (4) mode of WinTitleMatchMode, but it is over my head.  Any help is appreciated.  Here is a small reproducer script:
      Opt("WinTitleMatchMode", -1) Run("Notepad") WinWait("[CLASS:Notepad]", "", 10) Send ("Smith" & @CRLF & "Jones" & @CRLF & "Nom De Plume" & @CRLF) If WinExists("[CLASS:Notepad]", "jones") Then MsgBox(0,"FOUND","Found Jones") EndIf If WinExists("[CLASS:Notepad]", "nomdeplume") Then MsgBox(0,"FOUND","Found Nom De Plume") EndIf  
    • Chiitus
      Regex date
      By Chiitus
      Hello guys, i have a problem in this regex because its not groupping right.
      Look:
      (\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?) It checks for day or dayday, month or monthmonth, yearyear or yearyearyear+year.
      separated with or without space and the characters "\/.-"
      But, a problem! I tried to regex the date with or without the "separator + year" Eg: 05/05 or 05/05/05, but when i have 05/05/05, regex are separating 05/05 and /05
      Iam using group "()" wrong?
      Need help here
      Thx adv!
      ~~EDIT:
      Script used: (from autoitscript.com/forum/topic/129697-split-string-by-regular-expression/#comment-901335)
      #include <Array.au3> Local $sString = ClipGet() ; Extract the delimiters $aDelim = StringRegExp($sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)', 3) ; Split the string on the delimiters $aParts = StringSplit(StringRegExpReplace($sString, '(\d{1,2}\s?[\055|\056|\057|\134]\s?\d{1,2}(\s?[\055|\056|\057|\134]\s?\d{2,4})?)', "$<SEPARATOR>$"), "$<SEPARATOR>$", 3) ; Add the delimter to the start of the part For $i = 0 To UBound($aParts) - 1 ;$aParts[$i + 1] = $aDelim[$i] & $aParts[$i + 1] Next ; Set the count $aParts[0] = UBound($aParts) ; Display the result _ArrayDisplay($aParts) _ArrayDisplay($aDelim) Results in array: (the array parts in all string is ok; but the string delimitator himself is getting separated in every string that have year together)

      String used as test:
      Cheers.
    • TheDcoder
      Help with splitting a complex string
      By TheDcoder
      Hello , I am trying to find the best way to spilt this string:
      :<nick>!<host mask> PRIVMSG <channel/nick> :<message> into an array like this:
      Global Enum $SENT_BY, $HOST_MASK, $SENT_TO, $MESSAGE $aArray[$SENT_BY] = "<nick>" $aArray[$HOST_MASK] = "<host mask>" $aArray[$SENT_TO] = "<channel/nick>" $aArray[$MESSAGE] = "<message>" (Stuff between <> is dynamic)
      I can do that with inefficient code but I think it is possible to do it more efficiently with RegEx
       
      Thanks in Advance! TD
       
      P.S Yeah, that is the raw format of a message received in IRC
    • careca
      Stuck in Regex world
      By careca
      Hi ppl, im trying to do a script that reads an .au3 file, and retrieves all variables in it, then i work with that.
      This is what i got so far
      #include <Array.au3> $File = FileOpenDialog('Au3 Var Searcher', @ScriptDir, 'Au3Files (*.au3)') $FileOpen = FileOpen($File) $FileRead = FileRead($FileOpen) FileClose($FileOpen) Search() Func Search() Local $aArray = StringRegExp($FileRead, '$[:alnum:][:blank:]', 2) ConsoleWrite(' - '& $aArray[0] &' - '&@MSEC&@CRLF) _ArrayDisplay($aArray) EndFunc  
      So we know it has to start with $, and have [:alnum:] until [:blank:] or simply not [:alnum:] anymore. Or is there another way?
      Thanks in advance
       
    • Dgameman1
      I'm really stupid and can't figure out StringRegExp
      By Dgameman1
      I have a String that I'm trying to get the text between of.
      The string is something like
       
      user/Dgameman1" class="author  
      I'm trying to get everything between user/ and " class="author
      This is the code I currently have 
      StringRegExp($HTML,'(\/user\/)(.+?)(\" class)', 3) But for whatever reason, this isn't returning the value and I'm not sure how to go about fixing the Regex Expression.
       
      Thank you guys so much