As a result of my previous post, two interesting things happened. Firstly, several people pointed out the excellent ‘Ctrl+P’ command in VS Code, which left me feeling quite the chump for having spent time crafting my regex:
I mean, seriously, look at that – Almost everything I was trying to achieve, right within the product, a mere two keys away AND it matches on partial results. Egg is in proximity to my face.
There are some differences to the solution I suggested though, and this potential was recognised, picked up, and ran with by the preeminent @Waldo1001.
After some discussion on twitter, which you can see from here:
Waldo picked this up as an addition to his very handy CRS Extension for VS Code, and we also made a few tweaks the the regex. I thought I’d take the opportunity to explain those tweaks and how they work.
^\w+ (\d* )?"*
^\w+ (\d* )?"*(\w+ *)*
Firstly, Waldo suggested making the digit portion optional, which would allow this to search all object types including the ID-less ‘Interface’ objects.
To do this, we wrapped the digit portion in brackets to make it a group, then added a ? after the group to specify that the group must occur 0 or more times.
You may think ‘Why not just write make the number optional’, and if you did think that then you’re asking good questions.
However, you have to remember that the space characters are part of our pattern – in an object name such as:
table 27 Item
We have the pattern:
Word_Space_Number_Space_Word
If we remove that ID, we also remove a space character. Therefore we need to be looking for a combination of a number AND a space character together. Hence the group:
(\d+ )
Think of the group as a sub-regex within the bigger regex. We can now apply an operator to the group, rather than the individual operators, that specifies that group occurs zero or one times:
(\d+ )?
So now our regex will match ‘Table 27 Item’ as well as ‘interface “Price Calculation”‘
But wait!
We’ve gone back to matching far too many results.
The answer to that, lines in the fact that object names are always at the start of the file, and regular expressions have an operator that says ‘Look at the start of the file only’
Much better, eh?
So now, our regex looks like this:
^\w+ (\d+ )?"*
I have one more trick up my sleeve – what if you want to find objects where your word isn’t the first word in the name? What if when you search ‘Item’ you want to also return ‘Adjust Item Costs’?
We simply add at the end:
(\w+ *)*
This regex says to look for a group including a word and maybe a space, and to match 0 or more of those groups before our search term
This is effectively saying that after the double quote, which may or may not be there, also look for any number of, or none of, words, which themselves may or may not have a space after them, therefore allowing us to match things like ‘page 2314 “BC O365 Item List”‘ when we search for item
In closing, if you just want to quickly and easily search objects within VS Code, absolutely use Ctrl+P. However, if you want to learn regex, this isn’t a bad place to start. And if you want to be a bit more specific in what you search, or if object names don’t match object names for some reason, this may help.