Sign in to follow this  
Followers 0
orbs

RegExp - how to negate pattern

4 posts in this topic

#1 ·  Posted (edited)

i admit, i suck at RegExp. ever so often i take another shot at it, fail miserably to comprehend it, and write a dozen-lines function to do the thing. but this time i'm so close to get it... or so i think.

i need to replace a substring, but only if the substring is not prefixed by alpha-numeric character or underscore.

what i mean - examples:

in the following strings, the substring ABC should be replaced:

"ABC"

" ABC"

"(ABC)"

"+ABC"

in the following strings, the substring ABC should NOT be replaced:

"_ABC"

"AABC"

"1ABC"

what i have so far:

i managed to compose a pattern that does the opposite of what i need - it returns 1 if the prefix is alphanumeric or underscore. this is the pattern:

StringRegExp('ABC','[{1}:alnum:_]ABC')

currently using StringRegExp() for testing purposes, when i get it right, i'll move on to StringRegExpReplace()

i need help to negate the pattern. thanks for any hints...

 

EDIT: typo.

Edited by orbs

Share this post


Link to post
Share on other sites



Try this:

; using a negative look-behind pattern:
Local $aTest = ["ABC", " ABC", "(ABC)", "+ABC", "_ABC", "AABC", "1ABC"], $t

For $s In $aTest
    $t = StringRegExpReplace($s, "(?<!\w)(ABC)", 'xxx')
    ConsoleWrite("'" & $s & "' became '" & $t & "'" & @LF)
Next

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)

Share this post


Link to post
Share on other sites

that dark magic spell you cast... seems to do it! thanks!

now the hard part - me trying to figure out what exactly is that you did...  :think:

Share this post


Link to post
Share on other sites

Feed the pattern into http://regex101.com/ and the magic rabbit exhibits the answer:

/(?<!w)(ABC)/
  • (?<!w) Negative Lookbehind - Assert that it is impossible to match the regex below
    • w match any word character [a-zA-Z0-9_]
  • 1st Capturing group (ABC)
    • ABC matches the characters ABC literally (case sensitive)

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)

Share this post


Link to post
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
Sign in to follow this  
Followers 0