Using regular expressions to find a word with the five letters abcde, each letter appearing exactly once, in any order, with no breaks in between

前端 未结 3 1896
梦谈多话
梦谈多话 2021-02-13 06:45

For example, the word debacle would work because of debac, but seabed would not work because: 1. there is no c in any 5-character sequence tha

3条回答
  •  旧时难觅i
    2021-02-13 07:35

    Your solution is clever but unfortunately [a-e^...] doesn't work, as you found. I don't believe there is a way to mix regular and negated character classes. I can think of a workaround using lookaheads though:

        /(([a-e])(?!\2)([a-e])(?!\2)(?!\3)([a-e])(?!\2)(?!\3)(?!\4])([a-e])(?!\2)(?!\3)(?!\4])(?!\5)([a-e]))/
    

    See it here: http://rubular.com/r/6pFrJe78b6.

    UPDATE: Mob points out in the comments below, that alternation can be used to compact the above:

        /(([a-e])(?!\2)([a-e])(?!\2|\3)([a-e])(?!\2|\3|\4])([a-e])(?!\2|\3|\4|\5)([a-e]))/
    

    The new demo: http://rubular.com/r/UUS7mrz6Ze.

提交回复
热议问题