I’m building a small filter utility for users to quickly filter a list of items and I want to match the beginnings of words in order, preferably using regular expressions:
Consider a user trying to find the item labeled here is some text.
- I already know how to make it match the beginning of any one word:
her — here is some text — \bher
so — here is some text — \bso
ext — no match — \bext
- And I know how to make it match first letter of several words:
hist — here is some text — \bh.*?\bi.*?\bs.*?\bt
ht — here is some text — \bh.*?\bt
- What I need is the ability to match the first
ncharacters of several words:
herst — here is some text
iso — here is some text
teh — no match
I’m doing this because my items often contain intialisms, and a user may type usc to try and quickly pull up USA, California
I’m rewriting the pattern for each input, so I can do a little work then, as is necessary in case #2. I’m looking for a solution that will scale linearly with character count, in either pattern complexity or total complexity.
Given these constraints, what is my best option for matching these strings?
Monstrosities like:
Essentially, every letter is either preceded by the previous one, or a random sequence ending with a word boundary
(.*?\b). So, we make this random sequence + \b optional with?. So, breaking it up with(.*?\b)?between all the letters should work.