Search code examples
phpregexabbreviation

Regex - Matching Abbreviations of a Word


I was thinking in providing the following regex as an answer to this question, but I can't seem to write the regular expression I was looking for:

w?o?r?d?p?r?e?s?s?

This should match a ordered abbreviation of the word wordpress, but it can also match nothing at all.

How can I modify the above regex in order for it to match at least 4 chars in order? Like:

  • word
  • wrdp
  • press
  • wordp
  • wpress
  • wordpress

I'd like to know what is the best way to do this... =)


Solution

  • You could use a lookahead assertion:

    ^(?=.{4})w?o?r?d?p?r?e?s?s?$