konani 0 Posted April 30, 2010 Hi all,I read a string from a file, for example the string is possible one of some following string:(1) COPY source=d:\test\demo.txt destination=c:\demo.txt DeleteAfterCopy=1(2) COPY source=d:\test\demo.txt destination=c:\demo.txt(3) COPY source=d:\test\demo.txt destination=c:\demo.txt View=1In the first string, Regular Expression split the string into a suitable variable like: + $source = "d:\test\demo.txt"+ $destination = "c:\demo.txt"+ $deleteAfterCopy = 1In the second string, Regular Expression split the string into a suitable variable like: + $source = "d:\test\demo.txt"+ $destination = "c:\demo.txt"In the third string, Regular Expression split the string into a suitable variable like: + $source = "d:\test\demo.txt"+ $destination = "c:\demo.txt"+ $view = 1According to the string, it call a function like Copy($source, $destination, $deleteAfterCopy=0, $view=0). It means 2 variables $deleteAfterCopy and $view are optional.Help me make a regular expression to this case, please!I can solve this problem with another way by using StringInStr() and IF...ELSE statement to solve it. But I wanna do with Regular Expression.Thanks In Advance, Share this post Link to post Share on other sites
jchd 1,514 Posted April 30, 2010 Try this: #include <Array.au3> Local $test[3] = [ _ "(1) COPY source=d:\test\demo.txt destination=c:\demo.txt DeleteAfterCopy=1", _ "(2) COPY source=d:\test\demo.txt destination=c:\demo.txt", _ "(3) COPY source=d:\test\demo.txt destination=c:\demo.txt View=1" _ ] Local $a For $str In $test $a = StringRegExp($str, '(?i)source=(.+)\sdestination=(.+?)(?: (DeleteAfterCopy=1)| (View=1)|)$', 3) _ArrayDisplay($a) Switch UBound($a) Case 2 MsgBox(0, "Function called", "Copy($a[0], $a[1])") Case 3 MsgBox(0, "Function called", "Copy($a[0], $a[1], $a[2])") Case 4 MsgBox(0, "Function called", "Copy($a[0], $a[1], 0, $a[3])") EndSwitch ;~ Copy($source, $destination, $deleteAfterCopy=0, $view=0) 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 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) Share this post Link to post Share on other sites
konani 0 Posted May 1, 2010 (edited) Try this: #include <Array.au3> Local $test[3] = [ _ "(1) COPY source=d:\test\demo.txt destination=c:\demo.txt DeleteAfterCopy=1", _ "(2) COPY source=d:\test\demo.txt destination=c:\demo.txt", _ "(3) COPY source=d:\test\demo.txt destination=c:\demo.txt View=1" _ ] Local $a For $str In $test $a = StringRegExp($str, '(?i)source=(.+)\sdestination=(.+?)(?: (DeleteAfterCopy=1)| (View=1)|)$', 3) _ArrayDisplay($a) Switch UBound($a) Case 2 MsgBox(0, "Function called", "Copy($a[0], $a[1])") Case 3 MsgBox(0, "Function called", "Copy($a[0], $a[1], $a[2])") Case 4 MsgBox(0, "Function called", "Copy($a[0], $a[1], 0, $a[3])") EndSwitch ;~ Copy($source, $destination, $deleteAfterCopy=0, $view=0) Next Thank you, it's great! Edited May 1, 2010 by konani Share this post Link to post Share on other sites