Sublime Forum

Expand selection to everything between < and >

#1

I’m trying to find a way to expand selection to everything between the LESS THAN and GREATER THAN signs. For example I have some code like the following:

<p>test1<br>test2<br>test3<br></p>

And I’m trying to grab the text (test1, test2, test3) individually between each of the LT/GT symbols. I don’t see a built in feature for this but I was hoping that there could be a way to create a snippet or plugin for this.

Thank you.

1 Like

#2

Find with regex: (?<=>)[^<>]+(?=<)

1 Like

#3

Yes, that works great. Thank you.

Do you know if I could narrow that down to like everything between lines 50 - 70 for example?

0 Likes

#5

There is In selection switch in the find panel.
image

2 Likes

#6

Thank you again. Getting really close. Now I’m trying to exclude (from the regex selections) all selections between >< if there is nothing there. For example:

<p>test1<br>test2<br>test3<br></p>
<div>test1<br>test2<br>test3<br></div>

It is selecting the empty space between the
</p>
and the
<div>
from the 2nd line

Can we add an exclusion to the regex?

0 Likes

#7

(?<=>)(?!\s+<)[^<>]+(?=<)

3 Likes

#8

This is exactly what I needed.

Although for some reason, the ‘in selection’ option is not working all that well if the sublime FIND bar is already open. It seems like you need to hide it and then select text and THEN hit CTRL+F and then toggle the ‘in selection’ option to get it to work properly. If I’m doing something wrong, please let me know otherwise this is not a big issue.

Thanks for your help!

Edit: I used

"auto_find_in_selection": false

from this page: Find in selection behavior and it works great.

1 Like

#9

But, what if you have this new case? Seems that both regex of jfcherng will match all words, even those are not frame by tags

<p>test1<br> 

test2 <br>test3<br>

dfs</p>

asfsa asf as
0 Likes

#10

I don’t see OP mentioned that requirement. For ultimate “using a single regex to extract HTML content” questions, no you probably can’t or with a unreadable regex.

0 Likes

#11

everything is possible. With regex, or Python.

0 Likes

#12

In my particular case, this is what works:

(?<=>)([\w\d,-@()]|(\w|\d|,)\s(\w|\d|,))+?(?=<)

Although something much simpler would be like:

(?<=>)[\w\d]+?(?=<)

0 Likes