re not getting all matches

Viewed 65

I'm having some issues with the text extraction from the following string:

'Amount\n2144-PL\n1XL 2XL 3XL\n2144-\nPL Navy Blue 2 2 2 6 $11.50 $69.00\nETK-2097K-PL PLUS-TOP - 
 BACK BUTTON TUNICS 95% RAYON 5% SPANDEX MADE IN USA\n1XL 2XL 3XL\nBlack 2 2 2 6 $12.00 $72.00\nTeal 
 2 2 2 6 $12.00 $72.00\nETK-2197-SW-\n1XL 2XL 3XL\nPL ETK-\n2197- H.Grey/Burgu… 2 2 2 6 $14.00 
 $84.00\nOff-White/Black 1 1 1 3 $14.00 $42.00\nETK-2143 Tops - 95% RAYON 5% SPANDEX MADE IN USA\nS M 
 L\nHeather Grey 2 2 2 6 $10.50 $63.00\nRoyal Blue 2 2 2 6 $10.50 $63.00\nRuby Red 2 2 2 6 $10.50 
 $63.00\nETK2186-GD- Tops-Stripe Solid-95% Rayon 5% Spandex Made in USA\nPL\n1XL 2XL 
 3XL\nBurgundy/Bur… 2 2 2 6 $11.00 $66.00\nIvory/Black 2 2 2 6 $11.00 $66.00\n2139 - WP-PL PLUS TOP 
 -95% RAYON 5% SPANDEX MADE IN USA\n1XL 2XL 3XL\nAs Shown 2 2 2 6 $9.50 $57.00\nETK-2228\nS M L\nETK- 
 \n2228 Off-White/Black 2 2 2 6 $9.50 $57.00\nETK-2149-PL\n1XL 2XL 3XL\nETK-\n2149- Taupe 2 2 2 6 
 $11.50 $69.00\nBACK\nORDERED\nWhite 2 2 2 6 $11.50 $69.00\nSub'

I'm looking for the \nOff-White/Black 1 1 1 3 $14.00 $42.00\n pattern to get all the items in the string but for some reason I'm not getting all the matches with the following regex:

item_quantity_price = re.compile(r"\n[A-Za-z0-9-_./\s]*\d[\s]\d[\s]\d[\s]\d[\s][$]\d\d\d?.\d{2}[\s] 
[$]\d\d\d?.\d{2}\n")

Any information will be appreciated.

Best regards.

EDIT: Using : \n[A-Za-z0-9-_./\s]*(?:\s\d+){4}\s\$\d+.\d+\s\$\d+.\d+\n

results: click

1 Answers

Use

item_quantity_price = re.compile(r'^[\w\s./-]*(?:\s\d+){4}(?:\s\$\d+\.\d+){2}$', re.M | re.A)

See proof. With re.A \w becomes equal to [A-Za-z0-9_] and you can shorten the expression.

Explanation

--------------------------------------------------------------------------------
  ^                        the beginning of the line
--------------------------------------------------------------------------------
  [\w\s./-]*               any character of: word characters (a-z, A-
                           Z, 0-9, _), whitespace (\n, \r, \t, \f,
                           and " "), '.', '/', '-' (0 or more times
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (4 times):
--------------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  ){4}                     end of grouping
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (2 times):
--------------------------------------------------------------------------------
    \s                       whitespace (\n, \r, \t, \f, and " ")
--------------------------------------------------------------------------------
    \$                       '$'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    \d+                      digits (0-9) (1 or more times (matching
                             the most amount possible))
--------------------------------------------------------------------------------
  ){2}                     end of grouping
--------------------------------------------------------------------------------
  $                        the end of the line
Related