字符串中的查找问题

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

字符串中的查找问题

free.wang
str='aaabbbccceddddd'
 
如果我要搜索字符串中的 e,   请问怎么表达? 谢谢

--
deSign thE  fuTure

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

warren-18
str.find('e')

在08-1-16,小龙 <[hidden email]> 写道:
str='aaabbbccceddddd'
 
如果我要搜索字符串中的 e,   请问怎么表达? 谢谢

--
deSign thE  fuTure

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

li liu-2
In reply to this post by free.wang

str='aaabbbccceddddd'
if 'e' in str:
    print "yes"


在08-1-16,小龙 <[hidden email]> 写道:
str='aaabbbccceddddd'
 
如果我要搜索字符串中的 e,   请问怎么表达? 谢谢

--
deSign thE  fuTure

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

free.wang
 谢谢~~ 原来那么简单~~  那re里的 search是什么作用?
 

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

limodou
2008/1/17 小龙 <[hidden email]>:
>  谢谢~~ 原来那么简单~~  那re里的 search是什么作用?
>
search可以在字符串的中间找到一个匹配的结果,而match是从字符串开始进行的。起点不同。

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

li liu-2
In reply to this post by free.wang

The re module provides regular expression tools for advanced string processing. For complex matching and manipulation, regular expressions offer succinct, optimized solutions:

re 模块为高级字符串处理提供了正则表达式工具。对于复杂的匹配和处理,正则表达式提供了简洁、优化的解决方案。

>>> import re
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.sub (r'(\b[a-z]+) \1', r'\1', 'cat in the the hat')
'cat in the hat'

When only simple capabilities are needed, string methods are preferred because they are easier to read and debug:

如果只需要简单的功能,应该首先考虑字符串方法,因为它们非常简单,易于阅读和调试。

>>> 'tea for too'.replace('too', 'two')
'tea for two'


在08-1-17, 小龙 <[hidden email]> 写道:
 谢谢~~ 原来那么简单~~  那re里的 search是什么作用?
 

_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to   [hidden email]
Detail Info: <a onclick="return top.js.OpenExtLink(window,event,this)" href="http://python.cn/mailman/listinfo/python-chinese" target="_blank"> http://python.cn/mailman/listinfo/python-chinese


_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

Jiahua Huang
给你贴个逻辑匹配字符串好了

def match(text, andwords=[], orwords=[], notwords=[]):
    '''匹配字符串
    '''
    for notword in notwords:
        if notword in text:
            return False
    for andword in andwords:
        if andword not in text:
            return False
    for orword in orwords:
        if orword in text:
            return True
    return True


>>> match('早上好啊,这里是火星', ['早上','火星'])
True
>>> match('早上好啊,这里是火星', ['早上','火星'], ['好啊','不好'])
True
>>> match('早上好啊,这里是火星', ['早上', '火星'], ['好啊', '不好'], ['很好'])
True
>>> match('早上好啊,这里是火星', ['早上', '火星'], ['好啊', '不好'], ['好啊'])
False
>>>
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

free.wang
In reply to this post by limodou
 能给个search的例子么~  谢谢
--
deSign thE  fuTure
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese
Reply | Threaded
Open this post in threaded view
|

Re: 字符串中的查找问题

limodou
2008/1/17 小龙 <[hidden email]>:
>  能给个search的例子么~  谢谢

建议看文档。

--
I like python!
UliPad <<The Python Editor>>: http://code.google.com/p/ulipad/
meide <<wxPython UI module>>: http://code.google.com/p/meide/
My Blog: http://www.donews.net/limodou
_______________________________________________
python-chinese
Post: send [hidden email]
Subscribe: send subscribe to [hidden email]
Unsubscribe: send unsubscribe to  [hidden email]
Detail Info: http://python.cn/mailman/listinfo/python-chinese