Sublime Forum

Help with regex find and code folding

#1

I have a 2 part question (warning – I am new to Sublime and rusty on my regex syntax):

  1. Is there a way to construct a search that selects only the text between an opening and closing tag? For example, I can use this regular expression:
<section^>]*?>(.*|\n*)*?(</section>)

to select everything including the tags, but I want to end up with only the text (which will be other sets of tags) between the opening and closing highlighted. The goal is to then fold the highlighted text.

  1. It seems like I should be able to do this more efficiently using RegReplace package, but my head is spinning trying to understand the terminology and which type of definition and parameters I would need to use to set this up. If anyone is willing to help me through that, or at least provide rough steps, I’d be very appreciative.

–JV

Bonus question: Can anyone explain why

<section^>]*?>.\n]*?(</section>)

doesn’t work instead of my regex above, which seems less than elegant.

0 Likes

#2

Add this into your key config file:

{ "keys": "ctrl+alt+enter"], "command": "expand_selection", "args": {"to": "tag"} }

Then press ctrl+shift+ or to ctrl+shift+] to fold or unfold selection.

0 Likes

#3

Thank you, iamntz. The expand selection to tag command gets very close to my goal. The part that’s a little awkward is that each time I do a find for the next tag, the tag itself becomes selected and the expand select to tag command only operates on the search term text ("<section") until I hit an arrow key to remove the selection/highlight.

0 Likes

#4

For finding text between tags this regex does trick:

[^’">\s]+))?)+\s*|\s*)/?>](http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx)

0 Likes

#5

stackoverflow.com/a/1732454

0 Likes