2

I know how to output lines of matched strings (result from find command) by simply using editor:MarkerNext():

function print_marked_lines()

    local ml = 0
    local lines = {}

    while true do
        ml = editor:MarkerNext(ml, 2)
        if (ml == -1) then break end
        table.insert(lines, (editor:GetLine(ml)))
        ml = ml + 1
    end

    local text = table.concat(lines)
    print(text)

end

What I don't know is how to output only matched strings (not whole line as with posted snippet). I assume there is solution as matched strings are highlighted and must have some property which would allow extracting them, but I guess Scintilla knowledge is needed as I couldn't find any reference in provided SciTE bindings.

Example screenshot for find/match all regex pattern "I \w+":

enter image description here

I want to output (print to output pane) all highlighted string parts

theta
  • 24,593
  • 37
  • 119
  • 159

1 Answers1

1

@theta, nasty question (at least it was for me) :)

The problem is that in the Scite GUI "find/replace" dialog, you use one regex syntax for match patterns, with backslash (say, \s); while in Scite lua functions, you use a different syntax for patterns, with percent sign (correspondingly, %s) - see my posting in Lua pattern matching vs. regular expressions - Stack Overflow. From there, you have these two references:

Correspondingly, code for your function ("to output (print to output pane) all highlighted string parts") would be:

function print_marked_lines()

  local sel = editor:GetSelText()

  for mymatch in sel:gmatch"I %w+" do -- note; a regex match!
    print(mymatch)
  end

end

Outputs this in output pane from your example text:

I don
I assume
I guess
I couldn

Hope this helps,
Cheers!

Community
  • 1
  • 1
sdaau
  • 36,975
  • 46
  • 198
  • 278
  • 1
    Thanks for your nice answer. Unfortunately that would imply knowledge in yet another pattern syntax - Lua's. IMHO `editor:findtext(regex)` should be used and matched lines be additionally parsed for scintilla indicators - that was something I didn't know how to do. OTOH, some time after my question, SciTE introduced *strip dialog* which makes many things easily possible: http://www.scintilla.org/SciTELua.html (described at the bottom of the page). For example here is screenshot of wrapping `sh` and using `grep` to do above job: http://i.imgur.com/XogF9.png – theta May 22 '12 at 15:38
  • Now reading linked page, seems like `editor:match(text, [flags])` generator is the easiest way to go – theta May 22 '12 at 15:51