1

如何在不使用复杂正则表达式的情况下搜索空格分隔的字符串。这是我的代码

import re
stringToQueryLevel1 = ['why this','why this code','why this code is complex']  
for i in range(stringToQueryLevel1.__len__()):
    if re.search('why this' , stringToQueryLevel1[i]) != None:
        rc = 0
    else:
        rc = 1

    print rc 
4

2 回答 2

2

这很简单

stringToQueryLevel1 = ['why this', 'why this code', 'why this code is complex']
for item in stringToQueryLevel1:
    print ' ' in item
于 2013-04-02T09:45:00.433 回答
0

试试下面的代码:

import re
stringToQueryLevel1 = ['why this','why this code','why this code is complex','TestTest'] 
for i in range(stringToQueryLevel1.__len__()): 
    if re.search('(\w+)\s(\w+)' , stringToQueryLevel1[i]) != None:
        print 'match found in %s'%(stringToQueryLevel1[i])
    else:
        print 'match not found in %s'%(stringToQueryLevel1[i])
于 2013-04-02T10:05:02.057 回答