Jump to content

[Solved] StringRegExp split using muliple criteria


Subz
 Share

Recommended Posts

Does anyone know how to split a string using multiple delimiters, returning both the values and delimiters withing the Array using StringRegExp?  For example:

;~ Split on " Not ", " And ", " Or "
$sString = ' Not $a = 1 And $b = 2 Or $b = 3'
$aArray = StringRegExp($sString,...)
;~ Returned Results
$aArray[0] = '$a = 1'
$aArray[1] = 'And'
$aArray[2] = '$b = 2'
$aArray[3] = 'Or'
$aArray[4] = '$b = 3'

At the moment I'm using

Local $aArray1 = StringRegExp($sString, '(?i) Or | And | Not ', 3)

Creating a new array using string split and then joining the two arrays together again

Local $aArray1 = StringSplit(StringRegExpReplace($sString, '(?i) Or | And | Not ', '******'), '******', 3)

Unfortunately regular expression isn't my forte.

Edited by Subz
Solved
Link to comment
Share on other sites

That isn't as easy as you seem to think, in the general case. A condition can be any valid AutoIt expression, which can be fairly complex in the real world.

To achieve what you want (again, in the general case), you need a parser. It is possible to build a complete AutoIt grammar parser using regexp but that task isn't for the faint heated.

For instance, how would you break down the following condition:

$a > 5 Or ($b = ($x ? (Not $c Or $d) : $e) And ($f Or Not _MyFct($g - 4, ' Not And Or Game', $h And Not $i) Or (Not $k Or ($l And Not $m)))) Or ($n Or $o And Not $n And $o)

Agreed, it's a little bit extreme but you must be prepared to eat that (again in the general case).

If your use case is simpler, please specify it more clearly by making explicit the grammar you want to process.

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)

Link to comment
Share on other sites

@jchd Thanks for the response, this is part of a software script I wrote and as mentioned it works fine, but thought there may have been a better alternative to my current method.  Basically I use SCCM database to write an Ini file with uninstall keys for a particular software product, I can add additional conditions to this ini.  I then call this during the start of the script to determine which criteria the system matches and to perform certain actions based upon that.

The SWQuery key is what I'm trying to put into an array, I add an "If" during the function, replace each Qryxx using the Queries section and execute the string.  I hope that makes sense, please let me know if you would like me to clarify.

Here is an example Ini File

;~ Upgrade older versions are present
[Upgrade Software Name]
SWQuery = Qry01 Or Qry02 Or Qry03 And Qry04

;~ New Install no other versions are present
[Install Software Name]
SWQuery = Qry01 = '' Or Qry02 = '' Or Qry03 = '' And Qry04

[Queries]
Qry01 = RegRead('HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}.01', 'DisplayVersion')
Qry02 = RegRead('HKLM64\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}.01', 'DisplayVersion')
Qry03 = RegRead('HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}.02', 'DisplayVersion')
Qry04 = @OSArch = 'x64'

 

Link to comment
Share on other sites

This ?

Local $sSWQueryUpdate = IniRead("test.ini", "Upgrade Software Name", "SWQuery", "error")
Local $sSWQueryInstall = IniRead("test.ini", "Install Software Name", "SWQuery", "error")

$sSWQueryUpdate = StringRegExpReplace($sSWQueryUpdate, "\b(Qry\d+)\b", 'Execute(IniRead("test.ini", "Queries", "$1", ""))')
ConsoleWrite($sSWQueryUpdate & @CRLF)
Local $bRresult = Execute($sSWQueryUpdate)
MsgBox(0, "", "Result for update: " & $bRresult)

$sSWQueryInstall = StringRegExpReplace($sSWQueryInstall, "\b(Qry\d+)\b", 'Execute(IniRead("test.ini", "Queries", "$1", ""))')
ConsoleWrite($sSWQueryInstall & @CRLF)
$bRresult = Execute($sSWQueryInstall)
MsgBox(0, "", "Result for install : " & $bRresult)

 

Edited by jguinch
Link to comment
Share on other sites

Instead of using QRyXX for the keys, you can use a syntax like this :

SWQuery = %Query1% Or %Q2% Or %query_03% And %Qry04%

and a code like this :

$sSWQueryUpdate = StringRegExpReplace($sSWQueryUpdate, "%([^%]+)%", 'Execute(IniRead("test.ini", "Queries", "$1", ""))')

 

Edited by jguinch
Link to comment
Share on other sites

  • 4 weeks later...

@jguinch Is this the best way to split the string from your last post, what I would like to do is execute each one of the expressions individually prior to running the full expression.  The return values will be logged and if we want to determine why a machine failed to install or upgrade we can view which expressions the system failed on.  Hope that makes sense :)  While the below works I'm not sure if this is the best pattern,

#include <Array.au3>
Local $sSWQueryUpdate = IniRead("filename.ini", "Upgrade Software Name", "SWQuery", "error")
$sSWQueryUpdate = StringRegExpReplace($sSWQueryUpdate, "%([^%]+)%", 'Execute(IniRead("filename.ini", "Queries", "$1", ""))')
$aSWQueryUpdate = StringSplit(StringRegExpReplace($sSWQueryUpdate, '( And | Or | Not )', '|'), '|')
_ArrayDisplay($aSWQueryUpdate)

 

Link to comment
Share on other sites

It's not easy to answer because it depends of the complexity of the queries (as said JCHD).

The following expression returns True :

If (1=1 Or 2=3) And 4=4

Apparently you don't need to know that 2=3 is False because the whole condition returns True.

If you want to check each condition before execution, I would do that :

;~ Upgrade older versions are present
[Upgrade Software Name]
SWQuery = (%Qry01% Or %Qry02% Or %Qry03%) And %Qry04%

;~ New Install no other versions are present
[Install Software Name]
SWQuery = (%Qry01% Or %Qry02% Or %Qry03%) And %Qry04%

[Queries]
Qry01 = RegRead('HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}.01', 'DisplayVersion') = ''
Qry02 = RegRead('HKLM64\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}.01', 'DisplayVersion') = '' 
Qry03 = RegRead('HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall\{Guid}.02', 'DisplayVersion') = ''
Qry04 = @OSArch = 'x64'
;~ #Include <Array.au3>

Local $sSWQueryUpdate = IniRead("test.ini", "Upgrade Software Name", "SWQuery", "error")
Local $sSWQueryInstall = IniRead("test.ini", "Install Software Name", "SWQuery", "error")

$aSWQueryUpdate = StringRegExp($sSWQueryUpdate, "%([^%]+)%", 3) ; extract the query's variables
Local $sSWQueryUpdateBoolean = $sSWQueryUpdate

;~ _ArrayDisplay($aSWQueryUpdate)
For $i = 0 To UBound($aSWQueryUpdate) - 1
    $sCondition = IniRead("test.ini", "Queries", $aSWQueryUpdate[$i], "error") ; read the condition
    $vRes = Execute($sCondition) ? True : False ; executes the condition
    $sSWQueryUpdateBoolean = StringRegExpReplace($sSWQueryUpdateBoolean, "%" & $aSWQueryUpdate[$i] & "%", $vRes) ; replace each query by its result
    ConsoleWrite("Condition " & $aSWQueryUpdate[$i] & " : " & ($vRes ? "SUCCESS" : "FAILURE") & @CRLF )
Next

ConsoleWrite($sSWQueryUpdateBoolean & @CRLF) ; Boolean condition
$iRes = Execute($sSWQueryUpdateBoolean)
ConsoleWrite("Result : " & $iRes)

 

Link to comment
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
 Share

×
×
  • Create New...