ur Posted August 12, 2015 Posted August 12, 2015 How to use regular expressions in StringSplit and StringInStr
jchd Posted August 12, 2015 Posted August 12, 2015 You can't. Use StringRegExp (see help there for pattern grammar) and/or StringRegExpReplace. 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)
ur Posted August 12, 2015 Author Posted August 12, 2015 I want to check whether the particular word exists in a file and not surrounded by any double quotes.Ex: b = "sunday is only holiday" Here the word we are searching is only but it should return false as the word is in double quotes. Can you please help me on this.
jchd Posted August 12, 2015 Posted August 12, 2015 Please stick to the first topic. 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)
Malkey Posted August 12, 2015 Posted August 12, 2015 (edited) How to use regular expressions in StringSplit and StringInStrHere is an example of using a regular expression in conjunction with StringInStr.Local $b = 'She said,"Sunday is only holiday". Once a week only.' Local $sSearchWord = "only" MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _ ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not". ' present outside of quotes.')Edit: Replaced RE pattern, "(['""])[^\1]*\1", with this one, "(['""]).*?\1", Edited August 14, 2015 by Malkey See Mikell post #9
kylomas Posted August 13, 2015 Posted August 13, 2015 @Malkey - Thanks for the lesson in using group references. I understand it now that I've seen it in use. I changed the ^\1 class to non-greedy to handle the case of unmatched quotes...;Local $b = 'She said,"Sunday is only holiday". Once a week only.' Local $b = 'She said,"Sunday is only holiday". Once a week only. But watch out for orphan "" or '' marks.' Local $sSearchWord = "only" MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _ ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*?\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not". ' present outside of quotes.')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
ur Posted August 13, 2015 Author Posted August 13, 2015 Local $b = 'She said,"Sunday is only holiday". Once a week only1. But watch out for orphan "" or '' marks.'Local $sSearchWord = "only"MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _ ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*?\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not". ' present outside of quotes.') The result is different here.It is matching the word only1 also for only.
ur Posted August 13, 2015 Author Posted August 13, 2015 @Malkey - Thanks for the lesson in using group references. I understand it now that I've seen it in use. I changed the ^\1 class to non-greedy to handle the case of unmatched quotes...;Local $b = 'She said,"Sunday is only holiday". Once a week only.' Local $b = 'She said,"Sunday is only holiday". Once a week only. But watch out for orphan "" or '' marks.' Local $sSearchWord = "only" MsgBox(0, "Result", 'The word "' & $sSearchWord & '" is ' & _ ((StringInStr(StringRegExpReplace($b, "(['""])[^\1]*?\1", ""), $sSearchWord)) ? ("") : ("not")) & _ ; Ternary selection of "" or "not". ' present outside of quotes.')kylomasThanks Malkey,Kylomas and everyone who posted the reply for using regular expression in StringInStr.I got regular expression for StringSplit also by Melba23https://www.autoitscript.com/forum/topic/129697-split-string-by-regular-expression/
mikell Posted August 13, 2015 Posted August 13, 2015 (edited) @MalkeyYou can't use a backreference inside a character class$str = ' aac "bbb" 111 "aac" ' $res = StringRegExpReplace($str, "(['""])([^\1]*)\1", "###") MsgBox(0, "ex1", $res)Edit"Backreferences, too, cannot be used inside a character class. The \1 in a regex like (a)[\1b] is either an error or a needlessly escaped literal 1" (Jan Goyvaerts) Edited August 13, 2015 by mikell
Malkey Posted August 14, 2015 Posted August 14, 2015 @mikellThanks for the heads up.From my observations (of a backreference in a "not character" class, "[^\1]"), there appears to be no visible error generated, and, the unnecessary escaped literal "1" is not acting as a "1". It is acting more like a dot, "." - matching any character. This explains why the added lazy qualifier, " [^\1]*?", works like, ".*?".It appears a backreference cannot be used inside a character class because is not supported at this time.
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