mcan
mcan

Reputation: 2082

Getting quantity and unit

I want to get bold parts in sentences below.

Examples:

SmellNice Coffee 450 gr

Clean 2 k Rice

LukaLuka 1,5lt cold drink

Jumbo 7 gutgut eggs 12'li

Espresso 5 Klasik 10 Ad

Expression below works well until to the 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)

I have added \s|$ end of the expression. Thinking that If the unit is not the last word then there should be a space after it. But it didn't work. Briefly, how can I capture all bold expressions?

Upvotes: 0

Views: 91

Answers (2)

Old Winterton
Old Winterton

Reputation: 120


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.
)

Upvotes: 0

Michael
Michael

Reputation: 5335

It works with brackets:

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

Upvotes: 1

Related Questions