python re模塊如何判斷字元串中包含某些特定字元如文件名中不能包含'','/'等字元,如何檢查

^

方法有很多,例如使用首尾位置標記^$+非法字元集[^]實現:

regex=r'^[^\/:*?"<>|]+$'#不能為空,回不能含有/:*?"<>|等字元答
tests=['abc_def',
'abc.def',
'abc/def',
'?"',
'']
matches=[iforiintestsifre.match(regex,i)]
print(matches)

還可以通過負向零寬斷言(?!)等方式實現。

⑵ 如何檢查字元串是否包含Python中列表中的元素

list_a=['1','a']
str_a='134edacf42r23f'
forainlist_a:
ifstr_a.count(a):
print"字元串中存在%s"%a

⑶ python 字元串是否包含指定字元

string.find( substr, [start, [end]] )

#返回調用對象中出現substr的第一個字母的標號,如果S中沒有substr則返回-1。start和版end作用就相當於在S[start:end]中搜權索

>>> str = "aaabbbbssssadasd"
>>> str.find("ab")
2

⑷ python如何判斷字元串的包含關系,比如 xuexi 包含於 xuexihao

#!/usr/local/bin/python
str1 = "xuerui"
str2 = "xueruihao"
if str1 in str2:
print "yes"
else:
print "no"

⑸ python 判斷字元串中是否含有英文

可以使用正則表達式來判斷,要使用到re模塊
代碼如下:
>>> import re
>>> s = 'safasdfas'
>>> s2 = '15130351535'
>>> rt = re.findall('[a-zA-Z]+', s)
>>> len(rt)
1
>>> rt
['safasdfas']
>>> rt = re.findall('[a-zA-Z]+', s2)
>>> rt
[]
>>> len(rt)
0
>>>

如果返回有內容,則說明包含有英文,如上例中s,如果為空列表則說明不包含,如s2

⑹ python判斷文件內是否存在某字元串

a = 'abc' #--------------------要查詢的字元內串容
with open('1.txt','r') as foo:
for line in foo.readlines():
if a in line:
print line

⑺ python判斷字元串包含關系

這有什麼問題嗎?

運行效果如下:

⑻ python3判斷是字元串中包含某些特定字元

#! /usr/bin/python # -*- coding: utf-8 -*- import re zhPattern = re.compile(u'[\u4e00-\u9fa5]+') #一個小應用,判斷一段文本中是專否包含屬簡體中: contents=u'一個小應用,判斷一段文本中是否包含簡體中:' match = zhPattern.search(contents) if match: print u'有中文:%s' % (match.group(0),) else: print u'沒有包含中文'

⑼ 在Python中,如何檢查字元串是否只包含某些字元

#假如你的某些字元是s和a
some_letter=["s","a"]
ss="sadsahchcdsc"
other_letters=[]
forsinss:
ifnotsome_letter.count(s):
other_letters.append(s)
flag=True
ifother_letters:
print"字元串含有別的字元",other_letters