1

以下のプログラムを少し変更して実行すると、エラーが発生します。

import sys,re
match=re.compile(r'aa[0-9]+AB')
while 1 :
    line=eval(raw_input('Enter the string to search' 'or' "press 'q' to Quit"))
    if line == 'q':
        print "you are quit from the program"
        break
    if  match.search(line):
        print 'Matched:',line
        print pat
        print 'found',match.group()
        print type(pat)
    else:
        print "no match"
        print type(pat)

入力:

'aa12AB'

O/P:

>>>  Matched: aa12AB
<_sre.SRE_Pattern object at 0x02793720>
found
Traceback (most recent call last):
  File "C:\Pyth`enter code here`on27\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Users\thangaraj\Desktop\python program\UK Training program\New to add labtop\regular exp\Script1.py", line 11, in <module>
    print 'found',match.group()
AttributeError: '_sre.SRE_Pattern' object has no attribute 'group'
>>> 
4

2 に答える 2

1

オブジェクトに割り当てる必要がありmatchます:

m = match.search(line)

その後:

m.group()
于 2013-08-22T11:58:16.153 に答える