Jump to content

Recommended Posts

Posted (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 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 Codefor 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 APIErrorLog.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 TaskSchedulerIE 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 stuffOnHungApp handlerAvoid "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"
:naughty:  :ranting:, 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

Posted
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  image.gif.922e3a93535f431de08b31ee669cc446.gif
autoit_scripter_blue_userbar.png

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

Posted (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 by MattyD
Posted (edited)
4 hours ago, MattyD said:

Just food for thought :)

...makes us all greedy  :D
One thought concerning the ungreedy character "?" I added in the pattern (didn't want to add it at first, then added it after 2nd thought)

(?i)1[^1]*?2

Please note that the result would be same using mLipok's text, with or without the ungreedy "?" , but...
I preferred to add it in case of a text getting superfluous "2" (like explained in your concerns), for example a text ending like this :

...
1g
2
yyy2zzz

Now the "?" becomes crucial :

* With "?" (ungreedy), the 3rd group matches the following, which seems correct according to OP's indications.

1g
2

* Without "?" (greedy), the 3rd group matches the following, which doesn't seem correct :

1g
2
yyy2
Edited by pixelsearch
typo

"I think you are searching a bug where there is no bug... don't listen to bad advice."

Posted

@mLipok You overcomplicate the required pattern.

#include <Array.au3>
    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, '(?i)1.\R2', $STR_REGEXPARRAYGLOBALMATCH)
    ConsoleWrite(_ArrayToString($aResult, @CRLF & @CRLF & @CRLF) & @CRLF )

Using (?is) and .*? allows the engine to match at 1 until including a 2.

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)

Posted (edited)

Why do we keep the (?i), when we don't need case insensitive matching?

My suggestion: 

(?m)^1\w\R2$

Of course this is only based on the limited testing data we have now.

You can always swing by https://regex101.com/, to test different patterns with the PCRE (Legacy) flavor selected, and compare time/steps for the matching. Even try the regex debugger feature, if you need to see how it matches step for step, and where your pattern might not match.

Edited by genius257

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
×
×
  • Create New...