Regex - Matching Abbreviations of a Word

2019-08-14 20:27发布

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... =)

4条回答
对你真心纯属浪费
2楼-- · 2019-08-14 21:03

What about php similarity checker functions?

查看更多
乱世女痞
3楼-- · 2019-08-14 21:08

You could use a lookahead assertion:

^(?=.{4})w?o?r?d?p?r?e?s?s?$
查看更多
欢心
4楼-- · 2019-08-14 21:23

i know this is not a regex, just for fun...

#!/usr/bin/python

FULLWORD = "wordprocess"

def check_word(word):
    i, j = 0, 0
    while i < len(word) and j < len(FULLWORD):
        if word[i] == FULLWORD[j]:
            i += 1; j += 1
        else:
            j += 1

    if j >= len(FULLWORD) or i < 4 or i >= len(FULLWORD):
        return "%s: FAIL" % word
    return "%s: SUCC" % word

print check_word("wd")
print check_word("wdps")
print check_word("wsdp")
print check_word("wordprocessr")
查看更多
劳资没心,怎么记你
5楼-- · 2019-08-14 21:24
if ( strlen($string) >= 4 && preg_match('#^w?o?r?d?p?r?e?s?s?$#', $string) ) {
    // abbreviation ok
}

This won't even run the regexp unless the string is at least 4 chars long.

查看更多
登录 后发表回答