再不工作,奇怪的人物

2024-06-17 10:16:50 发布

您现在位置:Python中文网/ 问答频道 /正文

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的共轭”中找不到单词“Conjugation”。 最后我打印了一个单词和一个字符串。 似乎不是“变位”,而是印刷字“xxxConjugation”,其中xxx是奇怪的字符。在

我怎么能解决这个问题。我还试着把这两个字符串都编码成“utf-8”。结果是一样的。 这些奇怪的字只出现在字的前面,是看不见的变戏法. 在


Tags: of字符串research单词printcompile共轭
3条回答

根据您提供的信息,您的问题不可重现:

>>> 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位]

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']

如果这不是你的疑问,请解释一下你到底想做什么。在

发布的代码应该可以找到匹配项。请注意,re.search返回一个MatchObject,您不应该直接打印出它匹配的文本:

print LtryM.group(0)

这应该打印“共轭”。在

相关问题 更多 >