Jump to content

stringregexp position in string?


Recommended Posts

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

Link to comment
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.

Link to comment
Share on other sites

  • Moderators

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

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

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

  • Moderators

Malkey,

Never could count! :)

Previous post amended, thanks. ;)

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

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

  • 3 months later...

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