0

只有当它不是引号(单引号或双引号或任何三引号)并且拼写正确(无法获得类 d() )时,您如何创建一个函数来获取“类”之后的单词

"class hi()"  > hi

"class hi(dff)"  > hi

"class hi   (  dff  )  :"  > hi 

"  class        hi       (  dff  )  :"  > hi 

"class hi"  > hi

"classf hi"  > Nothing

"fclass hi"  > Nothing

"'class hi(dd)'"  > Nothing

'"class hi(dd)"'  > Nothing

"'''class hi(dd)'''"  > Nothing

'"""class hi(dd)"""'  > Nothing

'"""\n\n\n\nclass hi(dd)\n\n\n\n"""'  > Nothing    

"'class' hi()"  > Nothing

使用循环创建太难了。如果有人可以提供帮助,那就太好了,谢谢。这个很有挑战性

4

4 回答 4

4

像这样的东西,也许?

from StringIO import StringIO
from tokenize import generate_tokens
from token import NAME

def classname(s):
    g = generate_tokens(StringIO(s).readline)   # tokenize the string
    it = iter(g)
    for toknum, tokval, _, _, _  in it:
        if (toknum == NAME and tokval == 'class'):
            return it.next()[1]

print classname("class hi(29):")
于 2012-06-09T19:54:34.953 回答
3
import re

def remove(reg, s, multiline=False):
    flags = [re.M, re.M | re.DOTALL][multiline]
    s,num = re.subn(reg, "", s, flags=flags)
    return s

def classname(s):
    s = remove("\"\"\".*?\"\"\"", s, multiline=True)
    s = remove("\'\'\'.*?\'\'\'", s, multiline=True)
    s = remove("\".*?\"", s)
    s = remove("\'.*?\'", s)

    res = re.search("(^|\s)class\s+(\w+)", s, flags=re.M)
    # print "*** {} -> {}".format(s, res.groups() if res else None)
    if res is None:
        return None
    else:
        return res.group(2)

我想使用 \b 而不是 (^|\s) 但它似乎不想工作?

我还整理了以下测试代码:

tests = [
    ("class hi()", "hi"),
    ("class hi(dff)", "hi"),
    ("class hi   (  dff  )  :", "hi"),
    ("  class        hi       (  dff  )  :", "hi"),
    ("class hi", "hi"),
    ("classf hi", None),
    ("fclass hi", None),
    ("'class hi(dd)'", None),
    ('"class hi(dd)"', None),
    ("'''class hi(dd)'''", None),
    ('"""class hi(dd)"""', None),
    ('"""\n\n\n\nclass hi(dd)\n\n\n\n"""', None),   
    ("'class' hi()", None),
    ("a = ''; class hi(object): pass", "hi")
]

def run_tests(fn, tests=tests):
    for inp,outp in tests:
        res = fn(inp)
        if res == outp:
            print("passed")
        else:
            print("FAILED on {} (gave '{}', should be '{}')".format(inp, repr(res), repr(outp)))
于 2012-06-09T19:43:54.173 回答
2

使用正则表达式:

pattern = re.compile(r"\s*class\s+(\w+)")

例如:

>>> line_to_test = "  class        hi       (  dff  )  :" 
>>> match = pattern.match(line_to_test)
>>> match
<org.python.modules.sre.MatchObject object at 0x3>
>>> match.groups()
('hi',)
于 2012-06-09T19:42:43.447 回答
0
  1. 删除所有用引号括起来的子字符串(即'"'''""")。
  2. 使用正则表达式来匹配表达式“class (name of class here)”。

您可能需要调整正则表达式以正确匹配类名的所有有效 Python 标识符:

import re
m = re.match("class ([\w]+)", "class hi")
print m.group(0)
于 2012-06-09T19:48:13.970 回答