Sign in to follow this  
Followers 0
brycetech

stringregexp position in string?

8 posts in this topic

Hi

lets assume you have ran a regex and have obtained three strings from the search.

assume these matches are:

test.missing

test1.missing

test.missing

1 and 3 are the same...

what means would you suggest to return the position in the search text of those strings?

What I am doing is performing a regex search on multiple files..the search returns a listview which contains the file name that matches the search in column 1 and the matched text in column 2.

When I double click on the row/column, the file opens..my next task is to make it highlight the current match I have doubleclicked. If I double click #3 above, I need to know the position in the text. I am not aware of any flag that returns this position. Have I missed this? or perhaps some suggestions on how to approach this

tia

BT

Share this post


Link to post
Share on other sites



This is what I would do - after the regexp, loop through the array and use StringInStr to get the positions. Count the number of times you encounter a particular match in order to increment the occurence. You may wish to use _ArrayUnique before searching for the positions because duplicates may exist such as in your example.

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

brycetech,

You could use the Offset parameter in StringRegExp like this to find the positions of all instances of the search string:

#include <array.au3>

Global $aArray[3]

$sString = "test.missing  test1.missing   test.missing"
;           |    |              |              |
;           1    6              21             36

$sFind = "missing"

$iAdjust = StringLen($sFind)

$iOffset = 1

For $i = 0 To 2
    $aTemp = StringRegExp($sString, $sFind, 1, $iOffset)
    $iOffset = @extended
    ; Offset is the end of the searchstring so  we need to reset to the start
    $aArray[$i] = $iOffset - $iAdjust
Next

_ArrayDisplay($aArray)

Any use? ;)

M23

Edited by Melba23
Cannot count - see below

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

That's interesting Melba. I wonder if there is any advantage to doing it that way. I'll have to figure that out later. ;)

Share this post


Link to post
Share on other sites

@M23

Close, but not quite right.

$sString = "test.missing  test1.missing   test.missing"
;...........|....|..............|..............|
;...........1....5..............20.............35 <- Script results
;...........1....6..............21.............36 <- counting the characters

Share this post


Link to post
Share on other sites

Malkey,

Never could count! :)

Previous post amended, thanks. ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I just ran a quick test and it appears that StringInStr is more than twice as fast with this particular example. However it is difficult to decide what is a fair test. With a more complicated regexp pattern using StringInStr could be annoying and the speed difference will vary. Also with this example, the regexp version requires more lines of code: so it's not entirely clear (but still very interesting nonetheless).

#include <Array.au3>

Global $aArray[3]

$sString = "test.missing  test1.missing   test.missing"
$sFind = "missing"
$iAdjust = StringLen($sFind)

$iTimer = TimerInit()
For $j = 1 To 100
    _REVersion()
Next
ConsoleWrite(">>> " & TimerDiff($iTimer) & " <<<" &@CRLF)

$iTimer = TimerInit()
For $j = 1 To 100
    _SISversion()
Next
ConsoleWrite(">>> " & TimerDiff($iTimer) & " <<<" &@CRLF)

Func _REVersion()
    $iOffset = 1
    For $i = 0 To 2
        $aTemp = StringRegExp($sString, $sFind, 1, $iOffset)
        $iOffset = @extended
        $aArray[$i] = $iOffset - $iAdjust
    Next
EndFunc

Func _SISversion()
    For $i = 0 To 2
        $aTemp = StringInStr($sString, $sFind, 1, $i +1)
        $aArray[$i] = $aTemp
    Next
EndFunc

Perhaps this is a fairer test.

#include <Array.au3>

Global $aArray[3]

$sString = "test.missing  test1.missing   test.missing"
$sFind = "missing"
$iAdjust = StringLen($sFind)
$iOffset = 1

$iTimer = TimerInit()
For $j = 1 To 1000
    _REVersion()
Next
ConsoleWrite(">>> " & TimerDiff($iTimer) & " <<<" &@CRLF)

$iTimer = TimerInit()
For $j = 1 To 1000
    _SISversion()
Next
ConsoleWrite(">>> " & TimerDiff($iTimer) & " <<<" &@CRLF)

Func _REVersion()
    For $i = 0 To 2
        StringRegExp($sString, $sFind, 1, $iOffset)
    Next
EndFunc

Func _SISversion()
    For $i = 0 To 2
        StringInStr($sString, $sFind, 1, $i +1)
    Next
EndFunc
Edited by czardas

Share this post


Link to post
Share on other sites

Is there a way for the StringRegExp function to just return a 2D array when asking for an array return with the 2nd dimension being the offset of each result?

Share this post


Link to post
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
Sign in to follow this  
Followers 0