Match string with number, unit and keyword python

I need the following pattern: Get string from "number" followed by any char NOT letter(except 'x'), followed keyword like (oz or g) followed by other keyword like (bags, boxes, pack). Here some string as example

40497 frozen fi ench fried potatoes organic 12/16 oz bags --> 12/16 oz bags

apples 8x18x3.4 oz ( 96g ) boxes --> 8x18x3.4 oz (96g ) boxes

8 red 12 green 15 - .1 oz (3g) pack --> 15 - .1 oz (3g) pack

I have this pattern (\d*\.?\d+)(\W)((|)?)(oz.).*(bag.|boxe.|pack.) but the results are:

16 oz bags

3.4 oz (96g ) boxes

.1 oz (3g) pack

Upvotes: 0

Views: 111

Answers (1)

Yasser Mohsen
Yasser Mohsen

Reputation: 1480

Replace this part (\d*\.?\d+) with [\d\.][\d\.x\-\s\/]+

[\d\.]: to make sure that the matching result starts with a digit or a dot (one character)

[\d\.x\-\s\/]+: then the remaining group of characters (one or more) before oz keyword could be any of:

  • digit
  • dot
  • "x" character
  • "-" character
  • space
  • slash

Upvotes: 1

Related Questions