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

前端 未结 5 1841
刺人心
刺人心 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.

    0 讨论(0)
  • 2021-02-12 20:10

    if you do want to use a regular expression

    result = [ w for w in vocab if re.search('^.{24}',w) ]
    

    the {x} says match x characters. but it is probably better to use len(w)

    0 讨论(0)
  • 2021-02-12 20:13

    \w will match letter and characters, {min,[max]} allows you to define size. An expression like

    \w{9,}
    

    will give all letter/number combinations of 9 characters or more

    0 讨论(0)
  • 2021-02-12 20:20

    ^.{8,}$

    This will match something that has at least 8 characters. You can also place a number after the coma to limit the upper bound or remove the first number to not restrict the lower bound.

    0 讨论(0)
  • 2021-02-12 20:22

    .{9,} for "more than eight", .{8,} for "eight or more"
    Or just len(w) > 8

    0 讨论(0)
提交回复
热议问题