[英]Python string in another string with some generic (foo) characters
說a = 'h.el.o'
,我如何檢查 a 是否包含在另一個字符串中,即 '.' 任何字符(任何字符,但每個“.”只有一個不同的字符)?
例如,a 將包含在 b = 'a h w el i o y' 中,而不會包含在 c = 'a h wq el i o y' 中
這是正則表達式模塊和在整個字符串中查找表達式的re.search
方法的完美候選者, .
作為正則表達式中的通配符單個字符(巧合?):
import re
a = 'h.el.o'
b = 'ahwelioy'
c = 'ahwqelioy'
print(re.search(a,b) is not None)
print(re.search(a,c) is not None)
結果:
True
False
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.