Jump to content

Request for mentoring on SRE


DxMxS
 Share

Go to solution Solved by mikell,

Recommended Posts

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

  • Moderators

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

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

  • Moderators

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! :D

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

  • Solution

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 ... :D

#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 by mikell
Link to comment
Share on other sites

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 by mikell
Link to comment
Share on other sites

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 by AZJIO
Link to comment
Share on other sites

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

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...