gruntydatsun Posted September 5, 2013 Share Posted September 5, 2013 Any help with this regular expression greatly appreciated. I have a string to search and want to wrap instances of the word derp like this <b>derp</b> unless it appears in these words @derp,derpina,derpatitis,derpmessage it sort of works but it falls over when you have the word 'headerpmessage' or @derp as exclusions. If something appears before the derp it fails. I'm a bit lost in how to do look behind and look infront here. Any help greatly appreciated. $string = "the derp we need and dave@derp.com and derpatitis " ;string i'm searching for derp $string &= "and headerpmessage we dont want when searching derp out" ;but not some variations of derp $term = "derp" ;term i'm looking for $exclusion = "@derp,derpina,derpatitis,derpmessage" ;variations i don't want detected $exclude = StringReplace($exclusion,",","|") ;create exclusion list delimited by | $pattern = '(?!' & $exclude & ')' & $term ;build exclusions regex $repl_with = '<b>' & $term & '</b>'; ;string to insert when match found $answer = StringRegExpReplace($string,$pattern,$repl_with) ;find pattern in string and replace with msgbox(1,"ANSWER",$answer) ;results ;the answer Link to comment Share on other sites More sharing options...
Realm Posted September 5, 2013 Share Posted September 5, 2013 (edited) How about this? $string = "the derp we need and dave@derp.com and derpatitis " ;string i'm searching for derp $string &= "and headerpmessage we dont want when searching derp out" ;but not some variations of derp $term = "derp" ;term i'm looking for $pattern = ' ' & $term & ' ' ;build exclusions regex $repl_with = '<b>' & $term & '</b>'; ;string to insert when match found $answer = StringRegExpReplace($string,$pattern,$repl_with) ;find pattern in string and replace with msgbox(1,"ANSWER",$answer) ;results ;the answer Edit: This will not include when followed by punctuation. Such as "derp." This can be fixed by converting $pattern to an array and utilizing the different terms and replacements in a loop. Basic Example: $string = "the derp we need and dave@derp.com and derpatitis " $string &= "and headerpmessage we dont want when searching derp out" $term = "derp" Local $aPattern[3][2] $aPattern[0][0] = ' ' & $term & ' ' $aPattern[0][1] = ' <b>' & $term & '</b> ' $aPattern[1][0] = ' ' & $term & '\.' $aPattern[1][1] = ' <b>' & $term & '</b>.' $aPattern[2][0] = ' ' & $term & '\!' $aPattern[2][1] = ' <b>' & $term & '</b>!' Local $answer = $string For $i = 0 To UBound($aPattern)-1 $answer = StringRegExpReplace($answer,$aPattern[$i][0], $aPattern[$i][1]) Next msgbox(1,"ANSWER",$answer) Hope this helps a bit. Happy Coding! Realm Edited September 5, 2013 by Realm My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry. Link to comment Share on other sites More sharing options...
jchd Posted September 5, 2013 Share Posted September 5, 2013 Let's try this: Local $string = "the derp we need and dave@derp.com and derpatitis " & _ "and headerpmessage we dont want when searching derp out" Local $answer = StringRegExpReplace($string, "\b(?<!@)(derp)\b", "<b>\1</b>") MsgBox(1, "ANSWER", $answer) 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
kylomas Posted September 6, 2013 Share Posted September 6, 2013 @jchd - Is a word boundry any char that is not a-z and A-Z? I'm trying to understand why the "@" is a word boundry necessitating the look behind assertion. Incidentally, nice job on the beta Helpfile for SRE related topics. Do you use Expresso at all? kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
mikell Posted September 6, 2013 Share Posted September 6, 2013 @kylomas '@' is a W character so b matches its position and the lookbehind is needed to ignore this match Link to comment Share on other sites More sharing options...
kylomas Posted September 6, 2013 Share Posted September 6, 2013 @mikell - Thanks Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
jchd Posted September 6, 2013 Share Posted September 6, 2013 Nespresso? What else? No I only drink genuine ristretto coffe from on-demand ground beans. Tasteful and low on caffeine. 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
kylomas Posted September 6, 2013 Share Posted September 6, 2013 @mikell - Got it now, any char that is not a digit, letter or underscore (thanks to the beta doc by jchd) Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
jchd Posted September 6, 2013 Share Posted September 6, 2013 Thanks for reading! 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 hereRegExp tutorial: enough to get startedPCRE 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 More sharing options...
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