正则表达式过滤器数字可被3整除

Mar*_*tin 1 python regex

我有一个逗号分隔的ID(数字)列表.我只需要获得可以被3整除的这些.

Example: i = "3454353, 4354353, 345352, 2343242, 2343242 ..."

Tim*_*ker 12

只是为了它:

reobj = re.compile(
    r"""\b            # Start of number
    (?:               # Either match...
     [0369]+          # a string of digits 0369
    |                 # or
     [147]            # 1, 4 or 7
     (?:              # followed by
      [0369]*[147]    # optional 0369s and one 1, 4 or 7
      [0369]*[258]    # optional 0369s and one 2, 4 or 8
     )*               # zero or more times,
     (?:              # followed by
      [0369]*[258]    # optional 0369s and exactly one 2, 5 or 8
     |                # or
      [0369]*[147]    # two more 1s, 4s or 7s, with optional 0369s in-between.
      [0369]*[147]
     )
    |                 # or the same thing, just the other way around,
     [258]            # this time starting with a 2, 5 or 8
     (?:
      [0369]*[258]
      [0369]*[147]
     )*
     (?:
      [0369]*[147]
     |
      [0369]*[258]
      [0369]*[258]
     )
    )+                # Repeat this as needed
    \b                # until the end of the number.""", 
    re.VERBOSE)
result = reobj.findall(subject)
Run Code Online (Sandbox Code Playgroud)

将找到字符串中可被3整除的所有数字.

  • 我见过一个似乎有用的东西(剧透,我猜):http://www.rubular.com/r/ZcRDblHg8M (4认同)
  • 这与许多有效数字不匹配,例如1122,2241等 (2认同)
  • DFA转换为稍微简单的正则表达式`([0369] |([147] [*] [258])|(([258] | [147]*[147])([0369]*| [258] [0369]*[147])([147] | [258] [0369]*[258])))*`:http://tinyurl.com/7trrs5j (2认同)

geo*_*org 9

如果你真的是指数字(而不是数字),这就像

 re.findall(r'[369]', my_str)
Run Code Online (Sandbox Code Playgroud)

对于数字列表,没有正则表达式很容易:

lst = "55,62,12,72,55"
print [x for x in lst.split(',') if int(x) % 3 == 0]
Run Code Online (Sandbox Code Playgroud)

  • 那不属于零吗? (7认同)
  • @Martin,你可能不应该使用"*10x*",至少是math.stackexchange.com,除非你想混淆别人. (2认同)