Sublime Forum

Extracting the Captures from `find_all()` in the `View()` Class


I am using self.view.find_all() in the simple plug in I build.
I created the RegEx as in

While in RegEx101 it captures exactly what I need it seems the Sublime Text engine returns the graoup.
Is there a way to extract only the digits as in the link?

As an example, assuming I have:

Some list...

Let's add [link][01].
We can add [another one][002].

Even [more][003]!

Here is my link.


I apply self.view.find_all("^[\s]{0,2}\[([\d]*)\]:") # See and I get:


While I expect:



As a side node: “Convert inline link to reference” is part of MarkdownEditing plugin



As far as I know and according to the documentation for the method, this gives you the matches of the regular expression as a whole (i.e. $0) and not arbitrary capture groups, since it needs to return a list of sublime.Region instances that match the regex and there could be arbitrarily many groups in each.

As such, I would do something like this if you want to use the regex method for this.

[view.substr(s).strip(' \n\t[]:') for s in view.find_all(r"^[\s]{0,2}\[([\d]*)\]:")]

That said, assuming that you’re working with a file that’s markdown, the syntax definition (at least the one built into Sublime) has already done the regex work for you of knowing about that part of the file, so:

>>> [view.substr(r) for r in view.find_by_selector('')]
['01', '002', '003']
1 Like


The scoping isn’t robust. See an example I could easily find (Probably can find more) -