获取数量和单位

2024-05-15 20:38:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在下面的句子中用粗体部分

示例:

闻起来很香的咖啡450克

清洁2K大米

LukaLuka1,5lt冷饮

巨无霸7号肠胃蛋12'li

5克拉西克浓缩咖啡10AD

下面的表达式在last two之前运行良好

\d+[.,]?\d*\s*[’']?\s*(gr|g|kg|k|adet|ad|lı|li|lu|lü|cc|cl|ml|lt|l|mm|cm|mt|m)

我在表达式的末尾添加了\s|$。认为如果单位不是最后一个单词,那么它后面应该有一个空格。但它不起作用。简而言之,我如何捕捉所有粗体的表达


Tags: 示例表达式liad句子last咖啡two
2条回答

x2 = (
"\d+" #digit
"[,'\s]" #space comma apostrophe
"[\d*\s*]?" #opt digit or space
"((gr)|g|(kg)|k|(adet)|([Aa]d)|(lı)|(li)|(lu)|(lü)|(cc)|(cl)|(ml)|(lt)|l|(mm)|(cm)|(mt)|m)" #all the weights to look for
"(\s+|$)" #it's gotta be followed with a space, or with end of line.
)

它与括号一起工作:

\d+[.,]?\d*\s*[’']?\s*(gr|g|kg|k|adet|ad|lı|li|lu|lü|cc|cl|ml|lt|l|mm|cm|mt|m)(\s+|$)

相关问题 更多 >