I want to do a regEx search backwards to find the first match above the caret and extract the resulting regEx groups.
I don't think I understand getTag properly... I can't even figure out how to get a count of the number of groups it contains!?
I've tried several different methods - only some of them find a match backwards and none of them return the groups... can someone give me a firm shove in the right direction please?
Not quite sure if I understand your question correctly.
getTag doesn't provide a functionality to retrieve "submatches" (results when using regex like (hello).(world) where hello would be the first and world the second "submatch".
If you still want to use getTag, than you have to loop over it and as soon as you receive
an empty string your done.
In order to retrieve this, I would, for example, use a match object and lastindex property.
Concerning the backward search, why not using current position as the end attribute and
let's say 0 or any less number than current position as the start attribute? When list of matches resturns than
either use reverse call to iterate backwards or use last index from list ([-1]) to get the
nearest match.
Cheers
Claudia
Last edit: CFrank 2017-07-27
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Backward regular expression searches are problematic. The Notepad++ user interface prohibits them directly (when you change "Search Mode" to "Regular Expression", the "up" direction is cleared and disabled). Perhaps that is why this code produces output:
@Claudia
The getTag function does appear to return the required submatches when searching forward, but not when searching backwards.
My requirement is to extract the function-name as one regEx group and the function-parameters as a 2nd group from this example line (at pos 6048 of my test doc)...
function CheckNeighbors(ent, itemname)
Looking specifically at the findText test in my original post above (now highlighted in red), the exact same search is run forwards and backwards... it finds the regEx match in both cases but getTags only returns the required groups in the forward run.
Given Sasumner's observation above, and my repeating of the issue in npp LuaScript, I've opted to npp/scintilla regex searchPrev to find the required line but then using python regex (searching forward!) to extract the required groups.
Last edit: Moon 2017-07-27
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
@Moon, After rereading my post I guess I was a bit unclear - yes, getTag(number) rerturns a submatch but there is no function to retrieve how many submatches were catched.
Sorry for confusion.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi.
I want to do a regEx search backwards to find the first match above the caret and extract the resulting regEx groups.
I don't think I understand getTag properly... I can't even figure out how to get a count of the number of groups it contains!?
I've tried several different methods - only some of them find a match backwards and none of them return the groups... can someone give me a firm shove in the right direction please?
Here is what I've tried...
Search forward finds the match and gives me the groups, search backwards gives me nuthin...
So I try with findText...
Search forward is good. Search backwards finds a match but getTag is not returning the groups:
and searchInTarget:
gives same result as findText =
last try was searchNext/searchPrev:
No joy here either:
Thanks.
Last edit: Moon 2017-07-27
Not quite sure if I understand your question correctly.
getTag doesn't provide a functionality to retrieve "submatches" (results when using regex like (hello).(world) where hello would be the first and world the second "submatch".
If you still want to use getTag, than you have to loop over it and as soon as you receive
an empty string your done.
In order to retrieve this, I would, for example, use a match object and lastindex property.
Concerning the backward search, why not using current position as the end attribute and
let's say 0 or any less number than current position as the start attribute? When list of matches resturns than
either use reverse call to iterate backwards or use last index from list ([-1]) to get the
nearest match.
Cheers
Claudia
Last edit: CFrank 2017-07-27
Backward regular expression searches are problematic. The Notepad++ user interface prohibits them directly (when you change "Search Mode" to "Regular Expression", the "up" direction is cleared and disabled). Perhaps that is why this code produces output:
editor.research('a', match_found, 0, 0, editor.getTextLength())
but this does not:
editor.research('a', match_found, 0, editor.getTextLength(), 0)
(assumes 'a' is in your document)
Last edit: Sasumner 2017-07-27
I forgot my "match_found" function from the previous post:
def match_found(m): print m.span(0)
Thanks both for looking at this.
@Claudia
The getTag function does appear to return the required submatches when searching forward, but not when searching backwards.
My requirement is to extract the function-name as one regEx group and the function-parameters as a 2nd group from this example line (at pos 6048 of my test doc)...
Looking specifically at the findText test in my original post above (now highlighted in red), the exact same search is run forwards and backwards... it finds the regEx match in both cases but getTags only returns the required groups in the forward run.
Given Sasumner's observation above, and my repeating of the issue in npp LuaScript, I've opted to npp/scintilla regex searchPrev to find the required line but then using python regex (searching forward!) to extract the required groups.
Last edit: Moon 2017-07-27
@Moon, After rereading my post I guess I was a bit unclear - yes, getTag(number) rerturns a submatch but there is no function to retrieve how many submatches were catched.
Sorry for confusion.