I have a list of about 120 thousand english words (basically every word in the language).
I need a regular expression that would allow searching through these words using wildcards characters, a.k.a. *
and ?
.
A few examples:
- if the user searches for
m?st*
, it would match for examplemaster
ormister
ormistery
. - if the user searches for
*ind
(any word ending inind
), it would matchwind
orbind
orblind
orgrind
.
Now, most users (especially the ones who are not familiar with regular expressions) know that ?
is a replacement for exactly 1 character, while *
is a replacement for 0, 1 or more characters. I absolutely want to build my search feature based on this.
My questions is: How do I convert what the user types (m?st*
for example) to a regular expression ?
I searched the web (obviously including this website) and all I could find were tutorials that tried to teach me too much or questions that were somewhat similar, but not enough as to provide an answer to my own problem.
All I could figure out was that I have to replace ?
with .
. So m?st*
becomes m.st*
. However, I have no idea what to replace *
with.
Any help would be greatly appreciated. Thank you.
PS: I'm totally new to regular expressions. I know how powerful they can be, but I also know they can be very hard to learn. So I just never took the time do to it...