Sublime Forum

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

#1

I am using self.view.find_all() in the simple plug in I build.
I created the RegEx as in https://regex101.com/r/iopdag/latest.

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.


[01]: 1.com
  [002]: 2.com
  [003]: 3.com

I apply self.view.find_all("^[\s]{0,2}\[([\d]*)\]:") # See https://regex101.com/r/iopdag/latest and I get:

[01]:
  [002]:
  [003]:

While I expect:

01
002
003
0 Likes

#2

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

0 Likes

#3

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('entity.name.reference.link')]
['01', '002', '003']
1 Like

#4

The scoping isn’t robust. See an example I could easily find (Probably can find more) - https://github.com/sublimehq/Packages/issues/2835.

0 Likes