Jump to content
Subz

[Solved] StringRegExp split using muliple criteria

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

Share this post


Link to post
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)

Share this post


Link to post
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'

 

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Excellent thanks @jguinch that works really well, I like the logic, I just need to make sure the key prefix is "Qry" but that shouldn't be an issue.  Thanks again.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

 

Share this post


Link to post
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)

 

Share this post


Link to post
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

  • Similar Content

    • By jmp
      i am trying to get number from string using this code :
      #include <IE.au3> $oIE = _IEAttach ("Edu.corner") Local $aName = "Student name & Code:", $iaName = "0" Local $oTds = _IETagNameGetCollection($oIE, "td") For $oTd In $oTds If $oTd.InnerText = $aName Then $iaName = $oTd.NextElementSibling.InnerText $iGet = StringRegExpReplace($iaName, "\D", "") EndIf Next MsgBox(0, "", $iGet) it was get number like 52503058
      But, I want to get only student code 5250. (Different student have different code, sometime its 3 digits, Sometime 4)

       
    • By BlueBandana
      Is there a way to output the regex matches into a file?
      I have a script to compare two files and check for regex matches.
      I want to output the matching regex of 'testexample.txt' to another file.
      #include <MsgBoxConstants.au3> #include <Array.au3> $Read = FileReadToArray("C:\Users\admin\Documents\testexample.txt") $Dictionary = FileReadToArray("C:\Users\admin\Documents\example.txt") For $p = 0 To UBound($Dictionary) - 1 Step 1 $pattern = $Dictionary[$p] For $i = 0 To UBound($Read) - 1 Step 1 $regex = $Read[$i] If StringRegExp($regex, $pattern, 0) Then MsgBox(0, "ResultsPass", "The string is in the file, highlighted strings: " ) Else MsgBox(0, "ResultsFail", "The string isn't in the file.") EndIf Next Next  
    • By therks
      So I have this pattern: 
      ^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$ And I'm expecting (depending on input) to get a 1, 2 or 3 index array (or @error for invalid input).
      But instead I get this:
      #include <Debug.au3> Func Test($String) _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+)|(\d+):(\d+)|(\d+):(\d+):(\d+))$', 1)) EndFunc Test('10') ; Results (normal, expected): ; Row 0|10 Test('10:20') ; Results (extra blank index): ; Row 0| ; Row 1|10 ; Row 2|20 Test('10:20:30') ; Results (three blank indices): ; Row 0| ; Row 1| ; Row 2| ; Row 3|10 ; Row 4|20 ; Row 5|30 Is this normal? Should I just code around it, or is there a better way to do what I'm looking for?
      I also tried reversing my regex, but it was even uglier results:
      #include <Debug.au3> Func Test($String) _DebugArrayDisplay(StringRegExp($String, '^(?:(\d+):(\d+):(\d+))|(\d+):(\d+)|(\d+)$', 1)) EndFunc Test('10') ; Results (yuck): ; Row 0| ; Row 1| ; Row 2| ; Row 3| ; Row 4| ; Row 5|10 Test('10:20') ; Results (slightly better): ; Row 0| ; Row 1| ; Row 2| ; Row 3|10 ; Row 4|20 Test('10:20:30') ; Results (nice): ; Row 0|10 ; Row 1|20 ; Row 2|30  
    • By Deye
      Hi,
      I want to add any needed conditions to the StringRegExp command so it can pull out only  "File.au3", "WinAPIFiles.au3", "Test.bmp" into the array
      #include <FileConstants.au3> #include <MsgBoxConstants.au3> #include 'WinAPIFiles.au3' #include "File.au3" ; Script Start - Add your code below here Local $bFileInstall = False ; Change to True and ammend the file paths accordingly. ; This will install the file C:\Test.bmp to the script location. If $bFileInstall Then FileInstall("C:\Test.bmp", @ScriptDir & "\Test.bmp") $sFile = FileRead(@ScriptFullPath) $aResults = StringRegExp($sFile, "(?i)(FileInstall\s*|include\s*)(.*)", 3) _ArrayDisplay($aResults) Thanks In Advance
      Deye
    • By FroVN
      i have a text : <Name>Jonh</Name>.<Age>15</Age>
      how i can get Jonh and 15 in one stringregexp? pls give me example
×
×
  • Create New...