mLipok Posted 10 hours ago Posted 10 hours ago (edited) I have such example: #include <Array.au3> _Example() Func _Example() Local $sTest = _ "1a" & @CRLF & _ "1b" & @CRLF & _ "2" & @CRLF & _ "1c" & @CRLF & _ "1d" & @CRLF & _ "2" & @CRLF & _ "1e" & @CRLF & _ "1f" & @CRLF & _ "1g" & @CRLF & _ "2" & @CRLF & _ "" Local $aResult = StringRegExp($sTest, '(?is)1.*?2', $STR_REGEXPARRAYGLOBALMATCH) ConsoleWrite(_ArrayToString($aResult, @CRLF & @CRLF & @CRLF) & @CRLF ) EndFunc ;==>_Example Could somebody be so nice and explain 2 things: Why the result is: 1a 1b 2 1c 1d 2 1e 1f 1g 2 I expected to get the following results. How to get: 1b 2 1d 2 1g 2 ? Edited 10 hours ago by mLipok Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24
argumentum Posted 8 hours ago Posted 8 hours ago Spoiler This is a classic regex trap! Let’s break down exactly why your current pattern is over-matching, and then look at how to get your expected output. 1. Why you got the first result Your current regex is: (?is)1.*?2 Here is what is happening under the hood: (?is): This enables case-insensitivity (i) and "dot-all" mode (s). Dot-all means the dot . matches everything, including newlines (@CRLF). 1: The engine looks for the first literal 1 it can find. In your string, that's the 1 in "1a". .*?: This tells the engine to match any characters lazily (as few as possible) until it hits the next token. 2: The engine stops matching as soon as it hits the first 2. Because it starts at "1a" and stops at the first "2", your first match group captures everything from 1a down to the first 2. Then it repeats the process starting from 1c, and then 1e. The lazy quantifier .*? doesn't mean "skip to the closest 1 before a 2"; it just means "stop at the very first 2 you see after you've already found a 1". 2. How to get your expected result To get only the 1 that immediately precedes the 2, you need to tell the regex engine: "Match a 1, but do not allow any other 1s to exist between it and the trailing 2." You can achieve this by replacing the lazy dot .*? with a negated character class or a negative lookahead. The Solution: Using a Negative Lookahead The cleanest way to handle this with multi-character lines and newlines is to ensure that while we scan ahead to find a 2, we never cross over another 1. Here is the updated AutoIt code: AutoIt #include <Array.au3> #include <StringConstants.au3> ; Required for $STR_REGEXPARRAYGLOBALMATCH _Example() Func _Example() Local $sTest = _ "1a" & @CRLF & _ "1b" & @CRLF & _ "2" & @CRLF & _ "1c" & @CRLF & _ "1d" & @CRLF & _ "2" & @CRLF & _ "1e" & @CRLF & _ "1f" & @CRLF & _ "1g" & @CRLF & _ "2" & @CRLF & _ "" ; The new pattern: matches '1', ensures no other '1' follows before the '2' Local $aResult = StringRegExp($sTest, '(?is)1(?:(?!1).)*?2', $STR_REGEXPARRAYGLOBALMATCH) ConsoleWrite(_ArrayToString($aResult, @CRLF & @CRLF & @CRLF) & @CRLF ) EndFunc ;==>_Example How the new pattern works: 1(?:(?!1).)*?2 1: Finds a literal 1 (e.g., the one in 1a). (?: ... )*?: A lazy non-capturing group that loops through characters one by one. (?!1).: This is the magic. Before matching the next character (.), it looks ahead to make sure it is not another 1. If it is another 1 (like when moving past 1a towards 1b), the match fails for 1a, forcing the engine to discard 1a and start the whole match over again at 1b. 2: Closes the match once it hits the 2. ..I don't know RegEx but if the answer is good, then is a good answer 👍 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting
pixelsearch Posted 4 hours ago Posted 4 hours ago 5 hours ago, mLipok said: Why the result is... I think it's because the smallest match you get with the ungreedy "?" in pattern concerns only the "2" found in the pattern. * With "?" you got 3 matches, each one starting with the first "1" found, ending with the closest "2" found (ungreedy stops at the closest "2" +++) * Without "?" the match would start at first "1" found, ending at... the last "2" found (greedy !) so it would return only 1 big match (instead of 3) corresponding to the whole text. 5 hours ago, mLipok said: I expected to get the following results. How to get... I tried this pattern and it seems to work : (?i)1[^1]*?2 It matches anything between "1" and "2" starting at the last "1" found before "2" . Also we don't need the "s" option anymore as there are no "." in the pattern Result, the one you expected : 1b 2 1d 2 1g 2 Hope it helps "I think you are searching a bug where there is no bug... don't listen to bad advice."
MattyD Posted 46 minutes ago Posted 46 minutes ago (edited) Yeah @pixelsearch's explanation is is correct. But to be fair I think the answer could vary depending on the behavior we're after. How shall we deal with an 11a, 12, 22, or 122 etc? Do the 1's and 2's "count" here? Do we only need to check for digits after a newline, or do we match anywhere in the string? Can text legally exist after a "1f" etc, but before a newline? Can we run into rogue digits there? Just food for thought Edited 44 minutes ago by MattyD
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