我需要检查某些字符,例如[!,:]是否在字符串中的引号内,而不是在它们之外
The quick "brown!" fox jumps ":over" the lazy dog - valid string
The quick! "brown!" fox jumps ":over" the lazy, dog - invalid string
我该如何检查?
答案 0 :(得分:0)
您可以查看regular expression,这对此非常有用。
import re
ifvalid = False
chars = ['!', ',', ':']
str1 = 'The quick "brown!" fox jumps ":over" the lazy dog - valid string'
nonquote = re.split('["].+?["]', str1)
quote = re.findall('["].+?["]', str1)
for word in quote:
for ch in word:
if ch in chars:
ifvalid = True
for word in nonquote:
for ch in word:
if ch in chars:
ifvalid = False
if ifvalid:
print 'valid'
else:
print 'invalid'
答案 1 :(得分:0)
你曾经使用过regular expressions吗?
您可以删除所有带引号的字词并查找代币。
import re
def all_tokens_quoted(string):
quoted_words = re.compile('".*?"')
tokens = re.compile('[!,:]')
no_quotations = quoted_words.sub('', string)
if tokens.search(no_quotations):
return False
return True
all_tokens_quoted('The quick "brown!" fox jumps ":over" the lazy dog')
>>> True
all_tokens_quoted('The quick! "brown!" fox jumps ":over" the lazy, dog')
>>> False
答案 2 :(得分:0)
如果没有正则表达式,请尝试:
text = 'The quick "brown!" fox jumps ":over" the lazy dog'
text = text.split('"')
quotes = [text[i] for i in range (len(text)) if (i % 2 == 1)]
not_quotes = [text[i] for i in range (len(text)) if (i % 2 == 0)]
print(quotes, not_quotes)
这给出了正确的输出:
['brown!', ':over'] ['The quick ', ' fox jumps ', ' the lazy dog']
然后,您可以将其中的每一个拆分为字符串,并查看它们是否包含字符。
valid = True #assume valid
for not_quote in not_quotes:
characters = list(not_quote)
for character in characters:
if character in ['!',',',':']:
valid = False
可以对引号中的字符串执行类似的验证。
答案 3 :(得分:0)
str1 = "The quick \"brown!\" fox jumps \":over\" the lazy dog - valid string"
str1_split = str1.split("\"")
for str in str1_split[1::2]:
if str.find("!")>0:
print "Found!"
break