Regular expression matching anything greater than eight letters in length, in Python

前端 未结 5 1851
刺人心
刺人心 2021-02-12 19:24

Despite attempts to master grep and related GNU software, I haven\'t come close to mastering regular expressions. I do like them, but I find them a bit of an eyesore all the sam

5条回答
  •  孤街浪徒
    2021-02-12 20:08

    You don't need regex for this.

    result = [w for w in vocab if len(w) >= 8]
    

    but if regex must be used:

    rx = re.compile('^.{8,}$')
    #                  ^^^^ {8,} means 8 or more.
    result = [w for w in vocab if rx.match(w)]
    

    See http://www.regular-expressions.info/repeat.html for detail on the {a,b} syntax.

提交回复
热议问题