Jump to content

Help with StringRegExp


benners
 Share

Go to solution Solved by mikell,

Recommended Posts

I have a function that finds specified strings in a larger string. Sometimes an array will be returned and this needs to be sorted in descending order.

The array elements are Alpha numeric and are sorted as strings wheras I would like them to be sorted as numbers. It would be a simple thing to loop through

the array removing the text (KB) and converting the remaining string numbers to actual numbers, then loop through again adding the text to the sorted array and that is probably

what I will do.

To save a bit of time I would like to alter the line

Local $aResults = StringRegExp($sString, '(?i)KB\d{6,}', $iFlag)

to remove the KB text after it has found the string i.e KB123456. This would give me a number sorted array, removing one loop, to which I would only need to add the KB string before returning.

I have tried fumbling through the different options of StringRegExp but cannot get what I want returned. I assume it's possible as RegExp seems to be the best thing ever :thumbsup:

but I'm to numb to comprehend the execution.

Could anyone offer additions to the pattern to remove the KB portion?

Thanks

#include <Array.au3>

Local $sString = 'Security Update for Windows 7 for x64-based Systems (KB2676562)|Security Update for Windows 7 for x64-based Systems (KB2724197)|' & _
'Security Update for Windows 7 for x64-based Systems (KB2799494)|Security Update for Windows 7 for x64-based Systems (KB2813170)|Security Update for Windows 7 for x64-based Systems (KB2859537)|' & _
'Security Update for Windows 7 for x64-based Systems (KB2872339)|Update for Windows 7 for x64-based Systems (KB2679255)|Update for Windows 7 for x64-based Systems (KB2882822)' & _
'Update for Windows 7 for x64-based Systems (KB982018)'

Local $array = _KBNumber_GetFromString($sString, 3)

_ArrayDisplay($array)

; #FUNCTION# ====================================================================================================================
; Name ..........: _KBNumber_GetFromString
; Description ...: Checks a string for KB article number format (KB123456, minimum six digits) and returns results based on flags
; Syntax ........: _KBNumber_GetFromString($sString[, $iFlag = 1[, $iReturn = 1[, $sDelim = '|']]])
; Parameters ....: $sCallingFunc        - A string value identifying the function that called this one (For logging)
;                  $iLine               - An integer value identifying the line number of the calling function
;                  $sString             - A string value to check for KBArticleNumbers
;                  $iFlag               - [optional] An integer value based on StringRegExp function flags (0, 1, 3). Default is 1.
;                  $iReturn             - [optional] An integer value determining returned format (string (0) or Array (1)). Default is 1.
;                  $sDelim              - [optional] A string value setting the delimiting character for the string. Default is '|'.
; Return values .: Success: returns as below
;                  $iFlag = 0 - StringRegExp returns 1 (match) or 0 (no match)
;                  $iFlag = 1 - StringRegExp returns an array containing first match
;                  $iFlag = 3 - StringRegExp returns an array containing all matching instances
;                  $iReturn = 0 - Function returns a delimited string
;                  $iReturn = 1 - Function returns an array
;                  Failure: Logs an error message and sets @error to 1
; Author ........: Benners
; Modified ......:
; Remarks .......:
; Related .......:
; Example .......: _KBNumber_GetFromString('KB123456|KB123457|KB123458', 3) (Returns an array of all matching instances)
;                  _KBNumber_GetFromString('KB123456|KB123457|KB123458', 3, 0) (Returns a delimited string of all matching instances)
; ===============================================================================================================================
Func _KBNumber_GetFromString($sString, $iFlag = 1, $iReturn = 1, $sDelim = '|')
    Local $sRunningFunc = '_KBNumber_GetFromString'
    Local $aResults = StringRegExp($sString, '(?i)KB\d{6,}', $iFlag)
    If @error Then MsgBox(0,'error', @error)

    ; if array is returned then sort descending
    If IsArray($aResults) Then _ArraySort($aResults, 1)

    ; If $iFlag = 0 or $iReturn = 1 (returns matched/no match Or returns array with first match depending on $iFlag)
    If Not $iFlag Or $iReturn Then Return SetError(0, 0, $aResults)

    Local $sResult = ''

    For $i = 0 To UBound($aResults) - 1
         ; Combine the array strings
        $sResult &= $aResults[$i] & $sDelim
    Next

    ; return delimited string
    Return StringTrimRight($sResult, StringLen($sDelim))
EndFunc   ;==>_KBNumber_GetFromString
Link to comment
Share on other sites

Hi benner,

I like to know why people are doing things :D

So why are you trying to do this?

 

It started of as part of another project and grew into its own. The program, when finished, scans a selected folder for Windows updates and checks if they match certain search terms i.e Windows 7 x64. Once the list is loaded it looks for the KB number on Microsofts Update Catalog and gets the update info. if no results are returned or the update is superseded it is moved from the folder ad the Kb number is added to a database.

I will use it for making sure the updates folder is current everytime new patches are released so when I build a new WIM I only add the current updates or ones needed for my system. Once working I'll see if it works for Office updates as well

 

Oh yes I forgot the sorting

Local $aResults = StringRegExp($sString, '(?i)KB(\d{6,})', $iFlag)
If IsArray($aResults) Then 
   For $i = 0 to UBound($aResults)-1
      $aResults[$i] = Number($aResults[$i])
   Next
   _ArraySort($aResults, 1)
EndIf

 

Thanks mikell, during my tinkering I never altered the (?i)KBd{6,} portion I was trying to remove the KB after the string was found :idiot:

I did the same as you for the sorting - the StringFormat.

Link to comment
Share on other sites

http://download.wsusoffline.net/

This program developped in autoIT is actually automaticly downloading all up to date official windows and office patches for a specific version. Maybe it can also help you :)

Edited by Neutro
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...