Home Download Buy Blog Forum Support

Help with regex find and code folding

Help with regex find and code folding

Postby jvo33 on Mon Dec 31, 2012 7:41 am

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:

Code: Select all
<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 <section> and closing </section> highlighted. The goal is to then fold the highlighted text.

2) 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

Code: Select all
<section[^>]*?>[.\n]*?(</section>)


doesn't work instead of my regex above, which seems less than elegant.
jvo33
 
Posts: 2
Joined: Mon Dec 31, 2012 5:34 am

Re: Help with regex find and code folding

Postby iamntz on Mon Dec 31, 2012 7:52 am

Add this into your key config file:

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


Then press ctrl+shift+[ or to ctrl+shift+] to fold or unfold selection.
iamntz
 
Posts: 908
Joined: Fri Apr 29, 2011 8:52 am
Location: Romania

Re: Help with regex find and code folding

Postby jvo33 on Mon Dec 31, 2012 9:04 am

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 <section> 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.
jvo33
 
Posts: 2
Joined: Mon Dec 31, 2012 5:34 am

Re: Help with regex find and code folding

Postby capnhud on Thu Jan 03, 2013 12:18 pm

For finding text between tags this regex does trick:

</?\w+((\s+\w+(\s*=\s*(?:".*?"|'.*?'|[^'">\s]+))?)+\s*|\s*)/?>
capnhud
 
Posts: 62
Joined: Thu Jan 14, 2010 11:57 am

Re: Help with regex find and code folding

Postby adzenith on Thu Jan 03, 2013 8:43 pm

adzenith
 
Posts: 1214
Joined: Mon Oct 19, 2009 9:12 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 20 guests