DxMxS Posted November 13, 2013 Share Posted November 13, 2013 Hello Guys, I recently discovered PCRE thanks to AutoIt. Yesterday a couple fellows from this board taught me how to find a specific string, keep it and get rid of the rest of the content in an edit control. Local $fwlID = StringRegExpReplace($eContent, '(?s).*?((FWLd*)|$)', '$2' & @crlf) (In this case the strings I wanted to keep start with FWL followed by numbers). Today I have a new challenge, there are duplicate FWL codes. I want to keep one ocurrence of each code and get rid of all duplicates. I have done some research on the web and this forum but have not been able to find a solution. Can someone please assist me and explain his/her proposed regEx for our future reference? Thanks in advance! Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 13, 2013 Moderators Share Posted November 13, 2013 DxMxS,No need for a RegEx. Get the values into an array as I did in the code I posted and use _ArrayUnique to remove duplicates. 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...
DxMxS Posted November 13, 2013 Author Share Posted November 13, 2013 Thanks Melba, I will definitely give that approach a try. However, I still would like to learn how to get the task done using a RegEx Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted November 13, 2013 Moderators Share Posted November 13, 2013 DxMxS,Beware of the facination of RegExes. As GEOSoft (who introduced me to them several years ago) used to say - one of the things you need to learn is when NOT to use them. RegExes are, by definition, used to to detect regularly occuring patterns in data - they are not designed to be a complete toolbox. In this case finding all lines that start "FWL" is exactly what they are meant for - detecting duplicates is another thing altogether. That said, one of the RegEx gurus will no doubt come along and post a solution in a minute! 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...
DxMxS Posted November 13, 2013 Author Share Posted November 13, 2013 Thanks Melba, It is really good for me and my coding to learn in which scenarios a given approach is optimal and in which it is not. I appreciate your comment. Link to comment Share on other sites More sharing options...
Solution mikell Posted November 13, 2013 Solution Share Posted November 13, 2013 (edited) DxMxS, The regex Melba posted meant "find all matches" , the regexreplace I wrote meant "fire all but matches" , in both cases the approach is similar Maybe what you want is possible using regex, but this looks like a headache to come while the simplest way is usually the best one In this case Melba's solution does the job perfectly #include <Array.au3> $array = StringRegExp($text, 'FWL\d*', 3) $new_array = _ArrayUnique($array) Edit Nonetheless ... #include <GUIConstantsEx.au3> $sText = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" & @CRLF & _ "FWL12345" & @CRLF & _ "FWL9875632" & @CRLF & _ "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" & @CRLF & _ "FWL12345" & @CRLF & _ "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "FWL45678965" & @CRLF & _ "FWL12345" & @CRLF & _ "FWL985612565" & @CRLF & _ "bbbbbbbbbbbbbbbbbbbbbbbbb" $hGUI = GUICreate("Test", 500, 500) $cEdit = GUICtrlCreateEdit($sText, 10, 10, 480, 400) $cButton = GUICtrlCreateButton("Filter", 10, 450, 80, 30) GUICtrlSetState($cButton, $GUI_FOCUS) GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE Exit Case $cButton Filter() EndSwitch WEnd Func Filter() $ret = StringStripWS(StringRegExpReplace(GuiCtrlRead($cEdit), '(?s).*?((FWL\d*)(?!.*\2.*)|$)', '$2' & @crlf), 3) GuiCtrlSetData($cEdit, $ret) EndFunc Edited November 13, 2013 by mikell DxMxS 1 Link to comment Share on other sites More sharing options...
DxMxS Posted November 13, 2013 Author Share Posted November 13, 2013 Understood. Thank you guys Link to comment Share on other sites More sharing options...
mikell Posted November 13, 2013 Share Posted November 13, 2013 (edited) Sorry I forgot the explanation in my edit for the regex to remove duplicates (?!.*2.*) is a negative lookahead which means : "if the rest of the string doesn't contain the backreference #2 (that to say : the part matched)" So the one retained among the duplicates will be the last one found Edited November 13, 2013 by mikell Link to comment Share on other sites More sharing options...
AZJIO Posted November 13, 2013 Share Posted November 13, 2013 (edited) http://autoit-script.ru/index.php/topic,4861.msg35295.html#msg35295 #include <GUIConstantsEx.au3> #include <Array.au3> $sText = "xxxxxxxxxxxxxxxxxxxxxxxxxxxx" & @CRLF & _ "FWL12345" & @CRLF & _ "FWL9875632" & @CRLF & _ "yyyyyyyyyyyyyyyyyyyyyyyyyyyy" & @CRLF & _ "FWL12345" & @CRLF & _ "zzzzzzzzzzzzzzzzzzzzzzzzzzzz" & @CRLF & _ "" & @CRLF & _ "" & @CRLF & _ "FWL45678965" & @CRLF & _ "FWL12345" & @CRLF & _ "FWL985612565" & @CRLF & _ "bbbbbbbbbbbbbbbbbbbbbbbbb" $aText = StringSplit($sText, @CRLF, 3) _ArrayDisplay($aText, 'aText') $aText = _ArrayRemoveDuplicates($aText) _ArrayDisplay($aText, '_ArrayRemoveDuplicates') Func _ArrayRemoveDuplicates(Const ByRef $aArray) If Not IsArray($aArray) Then Return SetError(1, 0, 0) Local $oDict = ObjCreate("Scripting.Dictionary") $oDict.CompareMode = 0 ; Flag to indicate if the operations should be case sensitive For $i In $aArray $oDict.Item($i) ; shown by wraithdu Next Return $oDict.Keys() EndFunc ;==>_ArrayRemoveDuplicates Edited November 13, 2013 by AZJIO My other projects or all Link to comment Share on other sites More sharing options...
kylomas Posted November 13, 2013 Share Posted November 13, 2013 (edited) AZJIO - The 3RD parm for stringsplit should be "3" to use the whole split argument. edit: How does the dictionary get connected to the array? Edited November 13, 2013 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
DxMxS Posted November 14, 2013 Author Share Posted November 14, 2013 Thank you all guys! Mikell, I tried your code - it is awesome!. Thanks! 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