我使用Python的re
模块捕获am中word color
的所有修饰符。英语(AmE)和Br。英语(BrE)。我成功地捕捉到了几乎所有的单词,除了以撇号结尾的单词。这个问题是从瓦特的书开始的。你知道吗
以下是示例文本:
Red is a color.
His collar is too tight or too colouuuurful.
These are bright colours.
These are bright colors.
Calorific is a scientific term.
“Your life is very colorful,” she said.
color (U.S. English, singular noun)
colour (British English, singular noun)
colors (U.S. English, plural noun)
colours (British English, plural noun)
color’s (U.S. English, possessive singular)
colour’s (British English, possessive singular)
colors’ (U.S. English, possessive plural)
colours’ (British English, possessive plural)
这是我的正则表达式:\bcolou?r(?:[a-zA-Z’s]+)?\b
说明:
\b # Start at word boundary
colou?r #u is optional for AmE
(?: #non-capturing group
[a-zA-Z’s]+ #color could be followed by modifier (e.g.ful, or apostrophe)
)? #End non-capturing group; these letters are optional
\b # End at word boundary
问题是colors’
和colours’
匹配到s
。撇号被忽略。有人能解释一下我的代码有什么问题吗?我在SORegex Apostrophe how to match?上研究了这个问题,以及关于转义'
和"
的问题。你知道吗
这是Regex101
提前谢谢。你知道吗
问题是
\b
是单词边界,而对于...lors’
,’
和后面的空格之间的位置不是单词边界,因为’
和空格都不是单词字符。不要使用\b
,而要使用lookahead来表示空格、句点、逗号或后面可能出现的任何内容:https://regex101.com/r/lB49Nr/3
问题是结尾
\b
。它的定义是’
不在\w
组中。 尝试删除结尾:\bcolou?r(?:[a-zA-Z’s]+)?
相关问题 更多 >
编程相关推荐