Jump to content

StringRegExpReplace -uncertain word length


Recommended Posts

The block of text just above the table you mention explains it all.  At least I've tried to make it as "simple" and understandable as possible, but as the inherent working of regex isn't the paradigm most users are used to in their daily programming language(s) it may need some chewing to swallow.

Greedy means that a repeated pattern will match the maximum subject input which doesn't cause the rest of the pattern to fail. That may need backtracking.

Lazy means that a repeated pattern will match the minimum subject input which doesn't cause the rest of the pattern to fail. That may need backtracking.

Possessive means that a repeated pattern will match the maximum subject input, but without ever backtracking backwards inside the "possessed" part. This may cause the rest of the pattern to fail.

_ArrayDisplay(StringRegExp("AbABAbabbbabaaabbAbAb", "(?i)([ab]+)(aba)(.*)", 1), "Greedy")
_ArrayDisplay(StringRegExp("AbABAbabbbabaaabbAbAb", "(?i)([ab]+?)(aba)(.*)", 1), "Lazy")
_ArrayDisplay(StringRegExp("AbABAbabbbabaaabbAbAb", "(?i)([ab]++)(aba)(.*)", 1), "Possessive")

I used uppercase to help you identify which substring was captured.

In the possessive case, the subpattern [ab]++ will match the whole subject, but the rest of the pattern is left with nothing to match, hence failing flat on its nose.

In general, the possessive qualifier or its father the atomic grouping (see help) is unavoidable when an unlimited subpattern is enclosed inside another unlimited subpattern and there are instances where no match is possible.  In such case the huge number of nested backtrackings required makes the regex extremely slow.  Consider this case where we're after an unbounded repeat of "words" (strings of letters) or integers.  Subjects which match this pattern will match very fast, but when they don't, the regex will have to try many (really many) nested split points in the subject before failing.

Local $s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", $t

$t = TimerInit()
For $i = 1 To 100
    StringRegExp($s, "(\w*|[-\d]*)*[xy]")
Next
ConsoleWrite("Without possessive qualifier: " & StringFormat("%7.4f\n", TimerDiff($t) / 1000))

$t = TimerInit()
For $i = 1 To 100
    StringRegExp($s, "(\w*+|[-\d]*)*[xy]")
Next
ConsoleWrite("With possessive qualifier   : " & StringFormat("%7.4f\n", TimerDiff($t) / 1000))

 

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

Nice tutorial to ease understanding of possessive quantifiers - though the few lines in the help file were explicit enough IMHO
BTW I personally appreciate the naming "docile" used on rexegg for usual greedy quantifiers (which allow backtracking if needed) against "possessive" ones (which do not)

 

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