2011-03-13 31 views
0
textfields="""Conjugation of suorittaa """ 
stry2= """Conjugation""" 
stryc=re.compile(stry2, re.DOTALL) 
print 'textfields=', textfields, ' stry2=', stry2 
LtryM=re.search(stryc, textfields); print 'LtryM', LtryM 

我找不到重新单词“”“共轭”“”“字符串”“”“共轭suorittaa”“”。 我结束了我打印的字符串。 出现,而不是“”“共轭”“”我打印的单词“”“xxxConjugation”“”,其中xxx是奇怪的字符。重新不工作,奇怪的字符

我该如何解决这个问题。我也尝试将这两个字符串编码为'utf-8'。结果是一样的。 这些奇怪的字符只出现在单词的前面,在conjug.py中不可见。

回答

0

AFAIK,searchmatch如果您有捕获组,则返回匹配对象。要获得您期望的行为,请使用findall

In [23]: textfields="""Conjugation of suorittaa """ 

In [24]: stry2= """Conjugation""" 

In [25]: ls = re.findall(stry2 , textfields) 

In [26]: ls 
Out[26]: ['Conjugation'] 

如果这不是您的查询,请详细解释您正在尝试做什么。

0

发布的代码应该找到匹配的罚款。需要注意的是re.search返回MatchObject,你不应该直接打印出来 - 你可能想打印出它,而不是匹配的文本:

print LtryM.group(0) 

这应打印“偶联”。

1

你的问题是不可复制的,基于信息和您提供:

>>> import re 
>>> textfields="""Conjugation of suorittaa """ 
>>> stry2= """Conjugation""" 
>>> stryc=re.compile(stry2, re.DOTALL) 
>>> print 'textfields=', textfields, ' stry2=', stry2 
textfields= Conjugation of suorittaa stry2= Conjugation 
>>> LtryM=re.search(stryc, textfields); print 'LtryM', LtryM 
LtryM <_sre.SRE_Match object at 0x0195A870> 
>>> LtryM.group(0) 
'Conjugation' 
>>> print repr(textfields) 
'Conjugation of suorittaa ' 
>>> print repr(stry2) 
'Conjugation' 
>>> 

自己尝试复制它,像它可以复制/粘贴上面的方式为SO,并显示正是我做了什么,结果如何。

[Python 2.7.1; Windows 7 32-bit]