benners Posted November 23, 2013 Share Posted November 23, 2013 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 Thanks Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 23, 2013 Moderators Share Posted November 23, 2013 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") EndIfPerhaps 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 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
benners Posted November 23, 2013 Author Share Posted November 23, 2013 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 More sharing options...
mikell Posted November 24, 2013 Share Posted November 24, 2013 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) Link to comment Share on other sites More sharing options...
benners Posted November 24, 2013 Author Share Posted November 24, 2013 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now