我们没有得到所有的比赛

2024-10-01 17:28:47 发布

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

我对从以下字符串提取的文本有一些问题:

'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'

我正在寻找\nOff-White/Black 1 1 1 3 $14.00 $42.00\n模式来获取字符串中的所有项,但由于某些原因,我没有获取与以下正则表达式的所有匹配项:

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

如有任何信息,将不胜感激

致以最良好的祝愿

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

结果: click


Tags: 字符串inplusblueblackplwhitemade
1条回答
网友
1楼 · 发布于 2024-10-01 17:28:47

使用

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

proof。随着re.A\w变为等于[A-Za-z0-9_],您可以缩短表达式

解释

                                        
  ^                        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

相关问题 更多 >

    热门问题