我想分析一个句子来检查一些条件:
a) If there is a period and it is followed by a whitespace followed by a lowercase letter
b) If there is a period internal to a sequence of letters with no adjacent whitespace (i.e. www.abc.com)
c) If there is a period followed by a whitespace followed by an uppercase letter and preceded by a short list of titles (i.e. Mr., Dr. Mrs.)
目前,我正在遍历字符串(行)并使用next()函数来查看下一个字符是空格还是小写等,然后我只是在行中循环。但是我该如何检查下一个、下一个角色是什么呢?我怎样才能找到以前的呢?你知道吗
line = "This is line.1 www.abc.com. Mr."
t = iter(line)
b = next(t)
for i in line[:len(line)-1]:
a = next(t)
if i == "." and (a.isdigit()): #for example, this checks to see if the value after the period is a number
print("True")
任何帮助都将不胜感激。非常感谢。你知道吗
您可以使用多个后续操作来获取更多数据
您可以通过将迭代保存到临时列表来获取以前的迭代
正则表达式是您想要的。你知道吗
由于要检查字符串中的模式,因此可以通过
re
库利用python对正则表达式的内置支持。你知道吗示例:
类似地,为要签入字符串的条件生成模式。你知道吗
您可以使用this简单工具在线生成和测试正则表达式
更广泛地阅读
re
库here相关问题 更多 >
编程相关推荐