Home Download Buy Blog Forum Support

Noob question: Nested search?

Noob question: Nested search?

Postby codjeepop on Wed Aug 21, 2013 6:47 pm

Hi, I don't know the proper terminology for this, but say I am searching a .css file for a navbar's font-size, is there an easy way to make this search specific? If I'm looking through the .css file, say, of Twitter Bootstrap, there are a lot of styles for the navbar, making it difficult to just browse, but if I search for 'font-size,' I will get everything outside of the navbar as well. I know I can select a given section and search, but even this can be cumbersome. Ideally, I could do some kind of 'nested search' for lack of a better term. Like '.navbar { font-size'
codjeepop
 
Posts: 1
Joined: Wed Aug 21, 2013 6:37 pm

Re: Noob question: Nested search?

Postby edubkendo on Thu Aug 22, 2013 2:31 pm

sure, you just need the proper regex.

If you are familiar with using something like
Code: Select all
.*
to represent a "wildcard" in a search, regex's, or regular expressions, vastly expands on that idea. Basically, instead of searching for your literal text, you can use a combination of symbols to describe how you want it to search. It's perfect for the kind of situation you describe. Most modern programming languages include some sort of regex type, for doing string manipulation. And any decent text editor will employ them to some degree in their find-and-replace functionality. Sublime Text uses on of the best regular expression libraries on the planet, Oniguruma for powering it's syntax highlighting and some of its other capabilities. I am not sure, and hopefully someone who knows will chime in, but I would imagine that is also what is behind its Find and Replace tool also.

In Sublime Text, when you bring up the Find bar, there will be a section on the left for choosing some options and one of those is Regex.

Image

If you select it, Then you can type a regex in there instead of just characters to search for, and it will apply your regex. So, for the example you give. You should be able to get close with something like:

Code: Select all
(?<=navbar\s.)(\s.*\s)*\s(font-size)


This won't actually select just your word, but it will get you in the ballpark. Your particular use case turns out to actually be kind of tricky. At least for me. I'm sure someone better at regex than I am can tell you exactly what you want though.
edubkendo
 
Posts: 30
Joined: Fri May 18, 2012 2:43 pm
Location: Nashville

Re: Noob question: Nested search?

Postby qgates on Fri Aug 23, 2013 1:14 am

Likewise, in the regex approach, the following will get you to a similar place:

Code: Select all
\.navbar.*\{(.|\n)*font


ie. selects the text beginning .navbar and ending in font. Escape (optionally followed by End) gets you editing in the right place.

The alternative is a compound of two searches; search ".navbar" then search "font-size".

<rant>I really wish Sublime's macros would work with everything (including S&R for example) instead of just text edits/cursor movements. It's one of Sublime's biggest editing limitations presently imo. In my old editor I had a whole bunch of such macros saved which I could run from a "command" mode or bind to a keystroke. In the absence of this if you want this kind of thing often, it's best to write a simple plugin.</rant>

:)
qgates
 
Posts: 230
Joined: Mon Nov 05, 2012 4:33 pm

Re: Noob question: Nested search?

Postby edubkendo on Fri Aug 23, 2013 4:03 am

Adding to my earlier answer a little:

Code: Select all
(?<=navbar\s.)(?<!\})(\s.*\s)*\s(font-size)


This ensures that a navbar followed by a font-size in another set of brackets further down won't trigger it.
edubkendo
 
Posts: 30
Joined: Fri May 18, 2012 2:43 pm
Location: Nashville


Return to General Discussion

Who is online

Users browsing this forum: Exabot [Bot], jdaw1 and 22 guests