USSSR Posted January 26, 2022 Share Posted January 26, 2022 Hello all, I have been trying to figure out how to use StringRegExp flagging. This would be a small part of my very long code. Tried to search the forum, google and youtube but no luck. 😥 At first to get started I would need to use StringRegExp to find a string which would have 9 digits. For example "475910324". If this is found then I would need flagging to show/use it. How is it done? #include <GuiConstants.au3>;Functions needed for most GUI Code #include <Constants.au3> Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890" If StringRegExp($sData1,"\d9") then ConsoleWrite("Match found in Data1: " & $sData1&@CRLF) ;How to express only the found expression not the whole text in $sData1? I would need only the "123456789" instead of complete $sData1. Else ConsoleWrite("Match not found" &@CRLF) EndIf Exit Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted January 26, 2022 Moderators Share Posted January 26, 2022 USSSR, I would use the $STR_REGEXPARRAYFULLMATCH flag to return an array of all matches: #include <StringConstants.au3> #include <Array.au3> Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890" $aRet = StringRegExp($sData1,"\d{9}(?!\.)", $STR_REGEXPARRAYGLOBALMATCH) _ArrayDisplay($aRet, "", Default, 8) The pattern asks for all exact 9 digit strings, not followed by a decimal point to exclude the second case. M23 USSSR 1 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
USSSR Posted January 26, 2022 Author Share Posted January 26, 2022 28 minutes ago, Melba23 said: USSSR, I would use the $STR_REGEXPARRAYFULLMATCH flag to return an array of all matches: #include <StringConstants.au3> #include <Array.au3> Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890" $aRet = StringRegExp($sData1,"\d{9}(?!\.)", $STR_REGEXPARRAYGLOBALMATCH) _ArrayDisplay($aRet, "", Default, 8) The pattern asks for all exact 9 digit strings, not followed by a decimal point to exclude the second case. M23 Thanks for super quick response. You are very helpful. I have more to figure out but I have to think by myself a while if I find a solution... If I cant get it done I have to ask more help. Link to comment Share on other sites More sharing options...
jchd Posted January 26, 2022 Share Posted January 26, 2022 (edited) StringRegExp() help is your closest friend. Then RegExp is a very useful test bench. Edited January 26, 2022 by jchd USSSR 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
mikell Posted January 26, 2022 Share Posted January 26, 2022 3 hours ago, USSSR said: How is it done? A possible way, using word boundary \b Local $sData1 = "This is an example text which may include the correct thing 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits:1234567890" $res = StringRegExp($sData1,"\b\d{9}\b", 1) Msgbox(0,"", $res[0]) USSSR 1 Link to comment Share on other sites More sharing options...
USSSR Posted January 26, 2022 Author Share Posted January 26, 2022 Thanks for your support. My skills are not as good as I thought I cant make an if expression to work.. What is wrong here? #include <StringConstants.au3> #include <Array.au3> Local $sData1 = "This is an example text which may include the correct thing in most cases: tips 123456789. This is not 42345678.9 correct. This is not correct either as it contains 10 digits: 1234567890, this is not accepted either ddw123456789. " $aRet = StringRegExp($sData1, "\d{9}(?!\.)", $STR_REGEXPARRAYGLOBALMATCH) $bRet = StringRegExp($sData1, "(?i)tips") If $aRet = True and $bRet = True Then ConsoleWrite("TIPS AND NUMBER FOUND: " & $aRet[0] &@CRLF) ElseIf $aRet = True Then ConsoleWrite("ONLY NUMBER FOUND: " & $aRet[0] &@CRLF) Else ConsoleWrite("NUMBER NOT FOUND"&@CRLF) EndIf Exit Link to comment Share on other sites More sharing options...
jchd Posted January 26, 2022 Share Posted January 26, 2022 (edited) Regular expressions (RE) are in substance a series of arguments passed to a program (the RE engine), expressing what to search with explicit conditions. Hence it requires the same rigor to define formally (with mathematical rigor) what you consider to be a match. 4 hours ago, USSSR said: I would need to use StringRegExp to find a string which would have 9 digits. This phrasing from your first post doesn't seem to match what you tell us now. It looks like you need to match a series of exactly 9 digits preceeded and followed by whitespace(s), but only you can tell. Is that a correct definition for your use case? Edited January 26, 2022 by jchd USSSR 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
USSSR Posted January 26, 2022 Author Share Posted January 26, 2022 3 minutes ago, jchd said: Regular expressions (RE) are in substance a series of arguments passed to a program (the RE engine), expressing what to search with explicit conditions. Hence it requires the same rigor to define formally (with mathematical rigor) what you consider to be a match. This phrasing from your first post doesn't seem to match what you tell us now. It looks like you need to match a series of exactly 9 digits preceded and followed by whitespace(s), but only you can tell. Is that a correct definition for your use case? Hi, sorry for being unclear. Basically the first post is still correct and I need 9 digits to be preceded by a whitespace(s) but it can be followed by whitespace(s) or a "." This is not 100 % but will tackle out most of the cases. For making the code more "bulletproof" I would like to find 9 digits as above and a an additional word "tips". If both of these are found then code would continue and do its magic. If only the 9 digit number is found I will make a popup for me to make a choise what to do. The biggest challenge for me is how to declare(?) the 9 digits (if its found)... Link to comment Share on other sites More sharing options...
Nine Posted January 26, 2022 Share Posted January 26, 2022 20 minutes ago, USSSR said: What is wrong here? Only when the third parameter is 0 (or skipped), it will return a boolean, otherwise it returns an array...So testing an array to be true is not the best idea ! USSSR 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
jchd Posted January 26, 2022 Share Posted January 26, 2022 Would the string "blah fingertips match as well, when 987654321 is also there" match number and tips? Is "tips" always before number ? USSSR 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
USSSR Posted January 26, 2022 Author Share Posted January 26, 2022 17 minutes ago, jchd said: Would the string "blah fingertips match as well, when 987654321 is also there" match number and tips? Is "tips" always before number ? Good point! "tips" can be preceded by a whitespace(s) but it can be followed by whitespace(s) or a "." Link to comment Share on other sites More sharing options...
Solution jchd Posted January 27, 2022 Solution Share Posted January 27, 2022 Would this be a correct behavior? Local $aData = [ _ "This is an example text which may include the correct thing in most cases: tips 123456789.", _ "This is not 42345678.9 correct.", _ "This is not correct either as it contains 10 digits: 1234567890,", _ "this is not accepted either ddw123456789. ", _ "tips 111111111", _ "222222222 tips", _ "333333333", _ "tips.444444444", _ " 555555555 tips.", _ " tips 666666666.", _ " 777777777 ", _ " 888888888." _ ] For $s In $aData Local $a, $b $b = StringRegExp($s, "(?i)(?<= )tips(?= |\.)") $a = StringRegExp($s, "(?<= )(\d{9})(?= |\.)", $STR_REGEXPARRAYGLOBALMATCH) If @error Then ConsoleWrite("NUMBER NOT FOUND" & @CRLF) ElseIf $b Then ConsoleWrite("TIPS AND NUMBER FOUND: " & $a[0] & @CRLF) Else ConsoleWrite("ONLY NUMBER FOUND: " & $a[0] & @CRLF) EndIf Next USSSR 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
SOLVE-SMART Posted January 27, 2022 Share Posted January 27, 2022 (edited) Hi @jchd, I would say yes 👍 , but I am excited what the others will say.One little side note: Two example strings, [0] and [9], in the $aData array are basically the same as far as I can see. Best regards Sven ________________Stay innovative! Edited January 27, 2022 by SOLVE-SMART Stay innovative! Spoiler 🌍 Au3Forums 🎲 AutoIt (en) Cheat Sheet 📊 AutoIt limits/defaults 💎 Code Katas: [...] (comming soon) 🎭 Collection of GitHub users with AutoIt projects 🐞 False-Positives 🔮 Me on GitHub 💬 Opinion about new forum sub category 📑 UDF wiki list ✂ VSCode-AutoItSnippets 📑 WebDriver FAQs 👨🏫 WebDriver Tutorial (coming soon) Link to comment Share on other sites More sharing options...
USSSR Posted January 27, 2022 Author Share Posted January 27, 2022 3 hours ago, jchd said: Would this be a correct behavior? Local $aData = [ _ "This is an example text which may include the correct thing in most cases: tips 123456789.", _ "This is not 42345678.9 correct.", _ "This is not correct either as it contains 10 digits: 1234567890,", _ "this is not accepted either ddw123456789. ", _ "tips 111111111", _ "222222222 tips", _ "333333333", _ "tips.444444444", _ " 555555555 tips.", _ " tips 666666666.", _ " 777777777 ", _ " 888888888." _ ] For $s In $aData Local $a, $b $b = StringRegExp($s, "(?i)(?<= )tips(?= |\.)") $a = StringRegExp($s, "(?<= )(\d{9})(?= |\.)", $STR_REGEXPARRAYGLOBALMATCH) If @error Then ConsoleWrite("NUMBER NOT FOUND" & @CRLF) ElseIf $b Then ConsoleWrite("TIPS AND NUMBER FOUND: " & $a[0] & @CRLF) Else ConsoleWrite("ONLY NUMBER FOUND: " & $a[0] & @CRLF) EndIf Next Thanks this solves the problem. I just have to adjust it a bit to fit in to my code. Thank you for the help. Link to comment Share on other sites More sharing options...
USSSR Posted January 27, 2022 Author Share Posted January 27, 2022 On 1/26/2022 at 1:25 PM, jchd said: StringRegExp() help is your closest friend. Then RegExp is a very useful test bench. Thanks for the link to RegExp. Thats actually very good tool! Link to comment Share on other sites More sharing options...
jchd Posted January 27, 2022 Share Posted January 27, 2022 You'll note that the expression I posted is a large alternation: tips before of after number. In case you know tips will always be found before (resp. after) the number, you can simplify the expression by removing the unwanted after (resp. before) branch. SOLVE-SMART 1 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now