Jump to content
Subz

[Solved] StringRegExp split using muliple criteria

Recommended Posts

Subz

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
jchd

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
Subz

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

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
  • Like 1

Share this post


Link to post
Share on other sites
Subz

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
jguinch

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
  • Like 1

Share this post


Link to post
Share on other sites
Subz

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

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)

 

  • Like 1

Share this post


Link to post
Share on other sites
Subz

Thanks jguinch much appreciated.

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

    • Deye
      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
    • FroVN
      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
    • therks
      By therks
      I'm looking for a regex genius, cus I'm stumped when it comes to assertions.
      So what I have now, is this regular expression: ([^|=]+)=([^|]+)
      It takes a string (user input) of keys=values separated by pipes (ie: "param=value|param=value") and splits them into an array.
      Example:
      $vParamData = 'example=value|fruit=apple|phrase=Hello world' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; Result ; [0] => example ; [1] => value ; [2] => fruit ; [3] => apple ; [4] => phrase ; [5] => Hello world So that's working fine, but I'm wondering if there's also a way I could have this capture escaped pipes instead of splitting by them.
      ie:
      $vParamData = 'pipe test=this \| is a pipe|example=value' $aRegEx = StringRegExp($vParamData, '([^|=]+)=([^|]+)', 3) ; I'm getting this: ; [0] => pipe test ; [1] => this \ ; [2] => example ; [3] => value ; But I'd like a result like this: ; [0] => pipe test ; [1] => this \| is a pipe ; [2] => example ; [3] => value Is there some pattern that would accomplish this, or am I better off parsing it some other way?
    • Robinson1
      By Robinson1
      Well the plan is to use the power of regular expressions engine of AutoIT for patching binary data.
      Something like this: StringRegExp( $BinaryData,  "(?s)\x55\x8B.."
       
      <cut> ... Okay straight to question/problem
      ... certain bytes that are in the range from 0x80 to 0xA0 won't match.
      Hmm seem to be a char encoding problem. In detail these are 27 chars: 0x80, 0x82~8C, 0x8E, 0x91~9C, 0x9E,0x9F
      Here's a small code snippet to explore / explain this problem:
      #include "StringConstants.au3" $TestData = BinaryToString("0x7E7F808182") ;Okay $match = StringRegExp( $TestData ,'\x7E' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x7F' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x80' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Okay $match = StringRegExp( $TestData ,'\x81' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;Error no match $match = StringRegExp( $TestData ,'\x82' ,$STR_REGEXPARRAYFULLMATCH) ConsoleWrite('@extended = ' & @extended & ' $match = ' & $match & @CRLF) ;~ output: ;~ @extended = 2 $match = ;~ @extended = 3 $match = ;~ @extended = 0 $match = 1 ;~ @extended = 5 $match = ;~ @extended = 0 $match = 1 Hmm what to do? Go back and use the 'numberstring monster' implementation or just omit that range of 'unsafe bytes'. What is the root of this problem?
      Any idea how to fix this?
       
      Update: Okay I know a byte is not a character.
      But StringRegExp operates on String and so character level.
      Okay as long as you stay at Ansi encoding and only use /x00 - /X7F in the search pattern using  StringRegExp works well to search for binary data.
      What bytes can be matched that are in the range from /X7F - /xFF is also depending on the code page.
      So this avoid to search for bytes in the range from 0x80-0xa0 only applies to Germany.
      I just change this country setting:

      to Thai and now near all bytes from /X7F - /xFF fails to match.
    • Carm01
      By Carm01
      Hello,
      I have spent the past day fooling with StringRegExp to no avail attempting to get what would be a simple solution to an issue using StringRegExp.
      I will post the code in a sec. The string 'Java x Update y' where x and y are numeric values ONLY if a letter is mixed in anywhere then it should fail. I have been able to successfully deal with the x value so if x = 1234 or a1234 or 1a234 or 1234a would result in a fail if 'a' was in the string. However, when y = 1a234 then I get an output of 1 and when y = 1234a then the output = 1234 when both should fail. I am probably overlooking something simple and in looking through all the material and experimenting I am unable to figure it out and my experience with stringregexp and trying to find examples of this proved difficult. If someone could assist or point me to a thread ? Here is my code ; prob a simple fix. I am also trying to avoid white spaces.
      Thanks in advance
      #include <array.au3> $aArray = StringRegExp('Java 3009 Update 1a21', '(?i)Java (\d+) Update (\d+)', $STR_REGEXPARRAYGLOBALMATCH) If @error Then Exit _ArrayDisplay($aArray)  
×