Jump to content

Help with StringRegExp


benners
 Share

Recommended Posts

I am having a problem searching for multiple word patterns in a test string. I want to find any number of words in a string and they may occur in any order. After a few hours readingsearching the web and playing around I have a solution to do this if the search strings order matches the order the words occur in the test string, but I am unable to identify the pattern to achieve my main goal

The code I have is

Local $sString = 'Windows6.0-KB2803821-v2-x64.msu'
Local $sSearch = 'Windows6.0|x64'

$sSearch = '(?i)\b' & StringReplace($sSearch, '|', '\b.*\b') & '\b.*'
;~ MsgBox(0,'', $sSearch)
Local $vRet = StringRegExp($sString, $sSearch)

If @error then
    MsgBox(0,'Error', @error)
Else
    MsgBox(0,'Found', $vRet)
EndIf

If I Switch the delimited strings order a match is not found. What am I missing to fix this?. I am thinking there should be a ? in the pattern and have added  some hoping to stumble on a solution but tot no avail :idiot:

Thanks

Link to comment
Share on other sites

  • Moderators

benners,

Your specific case as posted can be solved by using positive lookahead/behind like this:

#include <Constants.au3>

Global $sString = "Windows6.0-KB2803821-v2-x64.msu"
Global $sSearch = "(Windows6.0.*(?=x64)|(?<=x64).*Windows6.0)"

If StringRegExp($sString, $sSearch) Then
    MsgBox($MB_SYSTEMMODAL, "Hi", "Found both")
Else
    MsgBox($MB_SYSTEMMODAL, "Error", "Both not found")
EndIf
Perhaps you could expand that to cater for "any number" of items, but I do not see how. :(

You will need to wait for one of the SRE gurus to show up. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Hi Melba.

Thanks for the reply. I looked at your code and had a play about with that and some other ideas I found  and came up with this. It seems to work as I want

Local $sString = 'windows6.0-KB2803821-v2-x64.msu'
Local $sSearch = 'Windows6.0|msu|x64|v2'

$sSearch = '(?i)^(?=.*\b' & StringReplace($sSearch, '|', '\b)(?=.*\b') & '\b)'
;~ MsgBox(0,'', $sSearch)
Local $vRet = StringRegExp($sString, $sSearch)

If @error then
    MsgBox(0,'Error', @error)
Else
    MsgBox(0,'Found', $vRet)
EndIf
Link to comment
Share on other sites

 

The tricky easy way could be to try a replace and check @extended for the number of replacements (meaning ok if > 0 )

Local $sString = 'windows6.0-KB2803821-v2-x64.msu'
Local $sSearch = 'Windows6.0|msu|x64|v2'

Local $vRet = StringRegExpReplace($sString, '(?i)(' & $sSearch & ')', "")
MsgBox(0,'result', @extended)

 

Hi mikell, thanks for the reply.

Your code works fine for the example, but there may be other search terms that cause the code to return incorrectly. As it uses StringRegExpReplace if I wanted to check for the stringword 'KB'  and the string was windows6.0-KB2803821-v2-KB-x64.msu it would count the KB in KB2803821 as a replacement as well.

An example would be, I would like to check a string to make sure all five of my search words were included. The StringRegExpReplace line would also count KB2803821, and a higher replacement count would be returned causing a return of 0

Local $sString = 'windows6.0-KB2803821-v2-KB-x64.msu'
Local $sSearch = 'Windows6.0|msu|x64|v2|KB'

MsgBox(0,'result', _String_SearchForWords($sString, $sSearch))

Func _String_SearchForWords($sTest, $sSearch)
    StringRegExpReplace($sSearch, '\|', 'r') ; count number of pipe chars
    Local $iCount = @extended + 1 ; add 1 to give the number of search words

    StringRegExpReplace($sTest, '(?i)(' & $sSearch & ')', "") ; count number of string replacements
    If $iCount = @extended then return 1 ; if they match all words were found
    Return 0
EndFunc

I finally decided on using this

Local $sString = 'windows6.0-KB2803821-v2-x64.msu'
Local $sSearch = 'msu|x64|Windows6.0|v2'

MsgBox(0,'', _String_SearchForWords($sString, $sSearch))

Func _String_SearchForWords($sTest, $sSearch)
    Local $sPattern = '(?i)^(?=.*\b' & StringReplace($sSearch, '|', '\b)(?=.*\b') & '\b)'
    Local $iRet = StringRegExp($sTest, $sPattern)
    If @error then Return SetError(1)
    Return $iRet
EndFunc
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...