Jump to content

RegEx - negative look behind failing


Go to solution Solved by mikell,

Recommended Posts

Posted

Why is the negative look behind group failing in this example?

$str = '<a href="http://example.com">link</a>' ; should NOT match
;$str = 'http://example.com' ; should match

$str = StringRegExpReplace($str, _
    '(?i)(?<!href=")(https?://[a-z0-9][a-z0-9.-]*[a-z0-9]\.[a-z]{2,4}|[a-z0-9][a-z0-9.-]*[a-z0-9]\.(?:biz|com|edu|eu|gov|info|mil|org|net|uk|us))(/\S*)*(?=\s|[[:punct:]]|$)', _
    '<a href="$1$2">$1$2</a>')

ConsoleWrite($str & @LF)

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Posted

In PCRE lookbehind assertions must have a fixed length. Sometimes K may be used to overcome this limitation.

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)

Hi jchd!

I understand that it must have a fixed length and it does... doesn't it?

(?<!href=")(https?

If i understand roughly how PCRE works, the 'matching point' is 'h' in the 'htttps?' - from there it should look at the previous 6 chars for 'href="'

I got done what i needed to do by adding a look-ahead group at the end and removing the neg. look-behind, but i still don't quite understand why the original failed to work, other than perhaps it has something to do with the anchor/matching point

$str = '<a href="http://example.com">link</a>' ; should NOT match
;$str = 'http://example.com' ; should match

$str = StringRegExpReplace($str, '(?i)(https?://[a-z0-9][a-z0-9.-]*[a-z0-9]\.[a-z]{2,4}|[a-z0-9][a-z0-9.-]*[a-z0-9]\.(?:biz|com|edu|eu|gov|info|mil|org|net|uk|us))(/\S*)*(?=\s|[[:punct:]]|$)(?!")', _
    '<a href="$1$2">$1$2</a>')

ConsoleWrite($str & @LF)
Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Posted

Ouch! Youre right of course. That is by far the most common reason for lookbehind to fail that I replied without looking enough.

What's your input?

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)

  • Solution
Posted (edited)

When oversimplifying the regex the lookbehind seems to work

$str0 = '<a href="http://example.com">link</a>' ; should NOT match
$str = StringRegExpReplace($str0, '(?i)(?<!href=")(https?://\w+\.\w+)', '<a href="$1">$1</a>')
Msgbox(0,"", $str0 & @crlf & $str)

$str1 = 'http://example.com' ; should match
$str = StringRegExpReplace($str1, '(?i)(?<!href=")(https?://\w+\.\w+)', '<a href="$1">$1</a>')
Msgbox(0,"", $str1 & @crlf & $str)

Edit

Got it. It's a problem with the first OR ( "|" ) which allows the match for its 2nd altenative, exactly like if your regex was

$str = StringRegExpReplace($str, '(?i)([a-z0-9][a-z0-9.-]*[a-z0-9]\.(?:biz|com|edu|eu|gov|info|mil|org|net|uk|us))(/\S*)*(?=\s|[[:punct:]]|$)',  '<a href="$1$2">$1$2</a>')

This should be corrected by the use of grouping parentheses to define precisely the alternatives for this OR

$str = StringRegExpReplace($str, _
    '(?i)(?<!href=")(https?://(([a-z0-9][a-z0-9.-]*[a-z0-9]\.[a-z]{2,4})|([a-z0-9][a-z0-9.-]*[a-z0-9]\.(?:biz|com|edu|eu|gov|info|mil|org|net|uk|us))))(/\S*)*(?=\s|[[:punct:]]|$)', _
    '<a href="$1">$1</a>')
Edited by mikell
Posted (edited)

Hi mikell

I must need a brain enlargement because i don't get it :)

So when the engine comes upon the "OR", are you saying content outside of the group is considered?

Essentially it is grouped like...

(?<!a)(b|c)(d)*(?=e)

So nothing should match if 'b' OR 'c' is preceded by 'a' and 'b' OR 'c' is not followed by 'e'

I'm not understanding how changing that to the following changes anything...

(?<!a)((b)|(c))(d)*(?=e)

This PCRE stuff is hard :)

Edited by iCode

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

Posted

iCode,

Take a minute to look at the result of a StringRegExp (not *Replace) to see that this is part c of the example which gets selected. You seem to rely on the anchoring of  the a part, but there is no anchor in your pattern.

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

Thanks gentlemen

@jchd - I began to suspect it was an anchor problem after reading more of the doc linked to in your sig

@mikell - that's not the actual replace expression i'm using, but yes, forgetting about captured groups has nearly bitten me before

FUNCTIONS: WinDock (dock window to screen edge) | EditCtrl_ToggleLineWrap (line/word wrap for AU3 edit control) | SendEX (yet another alternative to Send( ) ) | Spell Checker (Hunspell wrapper) | SentenceCase (capitalize first letter of sentences)

CODE SNIPPITS: Dynamic tab width (set tab control width according to window width)

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...