1

我有一个逗号分隔的 ids(digits) 列表。我只需要得到能被 3 整除的那些。

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

4

4 回答 4

13

只是为了它:

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)

将查找字符串中所有能被 3 整除的数字。

于 2012-06-12T07:52:36.497 回答
9

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

 re.findall(r'[369]', my_str)

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

lst = "55,62,12,72,55"
print [x for x in lst.split(',') if int(x) % 3 == 0]
于 2012-06-12T07:39:17.680 回答
2

使用这个问题的想法,我得到:

i = "1, 2, 3, 4, 5, 6, 60, 61, 3454353, 4354353, 345352, 2343241, 2343243"

for value in i.split(','):
    result = re.search('^(1(01*0)*1|0)+$', bin(int(value))[2:])
    if result:
        print '{} is divisible by 3'.format(value)

但是您不想为此任务使用正则表达式。

于 2012-06-12T08:02:33.710 回答
2

一个有希望的完整版本,来自减少 DEA[1]:

^([0369]|[147][0369]*[258]|(([258]|[147][0369]*[147])([0369]|[258][0369]*[147])*([147]|[258][0369]*[258])))+$

[1:] Converting Deterministic Finite Automata to Regular Expressions', C. Neumann 2005
注意:图 4 中有一个错字:从q_i到自身的转换应该读ce*bce*d.

于 2014-06-12T23:35:27.423 回答