如何扩展单词中的撇号(s'cream)?



这是我的单词列表。(实际上我正在使用一个大列表。

banana
fish
scream
screaming
suncream
suncreams

我想扩展s'cream.它必须仅与suncream匹配。

不匹配scream因为撇号没有字符。

不匹配suncreams因为末尾的 s 下落不明。

我编程不是很好,因为它只是匹配所有单词。

我试过了。这很尴尬。我不知道我在做什么。

find = "s'cream"
with open('words') as f:
for line in f:
word = line.strip()
skipchars = False
for c in find:
if c == "'":
skipchars = True
continue
if skipchars:
for w in word:
if c != w:
continue
if c not in word:
break
skipchars = False
print(word)

您可以使用更容易regex,将撇号替换为.+这意味着

  • .anychar
  • +1 次或更多次
import re
words = ['banana', 'fish', 'scream', 'screaming', 'suncream', 'suncreams']
find = "s'cream"
pattern = re.compile(find.replace("'", ".+"))
for word in words:
if pattern.fullmatch(word):
print(word)

使用正则表达式很容易:

使用w+的选择是与"单词"字符(如字母)匹配,并且要求至少映射 1 个字符。

import re
find = "s'cream"
words = [
"banana",
"fish",
"scream",
"screaming",
"suncream",
"suncreams"
]
target_re = re.compile("^{}$".format(find.replace("'", "w+")))
for word in words:
if target_re.match(word):
print("Matched:", word)
else:
print("Not a match:", word)
"""
output:
Not a match: banana
Not a match: fish
Not a match: scream
Not a match: screaming
Matched: suncream
Not a match: suncreams
"""

最新更新