Jump to content

Recommended Posts

Posted

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=1

In the first string, Regular Expression split the string into a suitable variable like:

+ $source = "d:\test\demo.txt"

+ $destination = "c:\demo.txt"

+ $deleteAfterCopy = 1

In 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 = 1

According 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,

Posted

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 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)

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 by konani

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...