I am trying to match words with up to two missing letters with regex. For example, if the word of interest is ‘hello’, I want to match the following strings:
hello
hell
helo
hllo
ello
hel
heo
elo
llo
I can use the regex h?e?l?l?o? to match these, but this will also match 0, 1, and 2 letter strings as well. How can I require the match to be 3-5 characters long?
You can use a look ahead to check for 3-5 of those characters:
Note that this will find a match like in a string like
helpsincehelpcontainshel. If you want to stp that you can check word boundaries or ends of string depending on your situation. If you want to match the ends of the sting add a^to the beginning and a$to the end. If you want to check word boundaries add\bto both ends.