Regular expressions python – get only the description V2

Question:

i am, again, trying to get description with RE Python modules, and i am almost done, but not everything, so..

I want to extract the description for this list;

list = ['Fatura Original-2ª via',
'Nº Z200 1/8206881085 Data 12-10-2022 Moeda EUR25505003116',
'NIF PT507399870 Cliente 25505 Código AT 12966332230',
'PLURAL - COOPERATIVA FARMACÊUTICA CR',
'Rua Manuel Madeira, nº35 - Apartado 8144',
'3025-047 Coimbra',
'Reg CRC Coimbra/NIF 500349142 - Capital social variável',
'Coimbra',
'Apoio ao cliente Website Farmácia Graça',
'239 499 405 www.plural.pt',
'UNIÃO DE FACTO, UNIPESSOAL LDA- FARMÁCIA GRAÇA',
'Pedidos/Encomendas Email Praça José Regalão Nº17',
'239 499 422 [email protected]',
'3140-011 Abrunheira',
'GBPt - Processado por programa certificado nº631/AT',
'Refª(s): 235230; 64169; 64170; 64172; 64180; 64181; 64182; 64188; 4190',
'Baque Cód. Designação Lote P.Val  QEnc QForn PVP PVF PVA Desc  Tx  rLiqUn IVA  Valor',
'  (MMAA) (a) Bónus Inf. %',
'110318 5192703 ACTIVELLE COMP REV 0,5MG + 0,1MG X 28 ME73780 1023 2  9,57 14,99 13,52 0,08 13,60A 6 27,19',
'205096 5260617 ATORVASTATINA AZEVEDOS MG COMP  20473 0424 2 2 9,77 ,53 6,82 55% 0,04 3,14T 6 6,27',
'80MG X 28',
'72750 5678800 ATORVASTATINA CICLUM MG COMP REV  12SXJD 0325 4 4 5,37 ,22 3,86 0,02 3,88A 6 15,52',
'40MGX28',
'110318 5419528 ATORVASTATINA TOLIFE MG COMP REV  ATD2206 0424 10 10 ,20 4,96 4,58 55% 0,02 2,11T 6 21,05',
'20MGX28',
'110318 2869584 BETAMOX PLUS COMP REV 875MG R012596R 0424 10 10 6,34 ,09 4,71 60% 0,02 1,93T 6 19,27',
'+125MG X 16',
'110318 5065487 BISOPROLOL SANDOZ MG COMP 5MG X 56 LX8098 1126 6 6 ,01 3,90 3,55 30% 0,02 2,53T 6 15,17',
'72750 5472949 GRIPONAL COMP EFERVESCENTE X 20 22DQ168 0325 10 10 ,74 ,74N 6 77,40',
'110318 5024575 JANUVIA COMP 100MG X 28 W016554 0525 6 6 41,90 32,87 9,96 0,16 30,12A 6 180,71',
'110318 5264528 LUMIGAN COLIRIO 0,1MG/ML 3ML E93716 1123 1 1 15,69 11,49 10,11 0,06 10,17A 6 10,17',
'72750 5658299 MIRTAZAPINA TOLIFE MG COMP REV  TB0165B 0225 1 1 5,22 4,08 3,73 40% 0,02 2,28T 6 2,28',
'15MG X 30',
'72750 5686225 OLMES MED+HTCZ KRKA CP REV 40MG DA5986 1024 1 1 17,78 13,38 11,95 58% 0,07 5,14T 6 5,14',
'+12,5X56',
'110318 5270061 PERINDOPRIL SANDOZ MG COMP 4MG X  NL5815 1224 1 1 6,11 4,88 4,51 30% 0,02 3,21T 6 3,21',
'30',
'205096 5796875 REDICT MG CAPS 0,5MG+0,4MG X30 LF27092A 0324 3 3 8,70 6,57 5,89 0,03 5,92A 6 17,77',
'110318 5726633 ROSUVASTATINA TAD MG COMP 10MG X  DB3294 0325 1 1 9,36 7,17 6,47 58% 0,04 2,79T 6 2,79',
'60',
 '72750 7747246 SOLUTO DAKIN SOL 0,5% 1L GSL 1 1 0,60 0,60N 6 0,60',
 '72750 5341235 TELMISARTAN SANDOZ MG COMP 20MG X  MC6116 0224 3 3 4,30 3,27 2,93 30% 0,02 2,09T 6 6,26',
 '28',
 '72750 8740605 TRAMAL SOL ORAL GOTAS 100MG/ML  01482T 0626 5 5 3,40 2,45 2,14 0,01 2,15A 6 10,76',
 '10ML',
 '110318 5169727 VENLAFAXINA MYLAN MG CAPS LP 150MG  8131512 1024 1 1 8,91 6,75 6,07 40% 0,03 3,71T 6 3,71',
 'X 30',
 'Total: 68 Sub-Total: 425,27',
 'Produtos não fornecidos:',
 '5164579 LOSARTAN BLUEPHARMA MG COMP 50MG X 60 Esgotado 5664867 PREGABALINA AUROVITAS MG CAPS 100MG X 84 Esgotado',
 'Nº total de baques:3',
 '110318 205096 72750',
 'Página 1 de 2Fatura Original-2ª via',
 'Nº Z200 1/8206881085 Data 12-10-2022 Moeda EUR25505003116',
 'NIF PT507399870 Cliente 25505 Código AT 12966332230',
 'PLURAL - COOPERATIVA FARMACÊUTICA CR',
 'Rua Manuel Madeira, nº35 - Apartado 8144',
 '3025-047 Coimbra',
 'Reg CRC Coimbra/NIF 500349142 - Capital social variável',
 'Coimbra',
 'Apoio ao cliente Website Farmácia Graça',
 '239 499 405 www.plural.pt',
 'UNIÃO DE FACTO, UNIPESSOAL LDA- FARMÁCIA GRAÇA',
 'Pedidos/Encomendas Email Praça José Regalão Nº17',
 '239 499 422 [email protected]',
 '3140-011 Abrunheira',
 'GBPt - Processado por programa certificado nº631/AT',
 'Informamos que não se aceitam quaisquer devoluções de produtos adquiridos no âmbito do Covid-19. ',
 'Art.36º,nº5,Al.f) CIVA - Os bens e serviços constantes deste documento foram colocados à disposição do adquirente na data do mesmo.',
 'Taxa Base Valor IVA APVA <= 50 EUR 262,12 Incidência 425,27',
 'NNet 78,00',
 '6 % 425,27 25,52 IVA 25,52',
 "TVD's+TO's+Tripartidos 85,15",
 '(a) PVA sem Taxa de Infarmed (Tx.Inf).  Total 450,79',
 'A fatura deve ser liquidada até 18.10.2022. ',
 'Após esta data 18.10.2022 serão cobrados juros de mora nos termos do DL nº 62/2013, de 10 de Maio. ',
 'O prazo para reclamação é de 5 dias após data de emissão. O documento é considerado confirmado no final desse período. ',
 'A responsabilidade pela gestão dos resíduos das embalagens e medicamentos foi transferida para a Entidade Gestora VALORMED - ',
 'Sociedade Gestora de Resíduos de Embalagens e Medicamentos, Lda.. Mais informações, incluindo os valores das prestações  Local Carga',
 'financeiras fixadas a favor daquela, em www.valormed.pt. ',
 'Para mais informações consulte a sua área reservada em www.plural.pt. Coimbra',
 'Local Descarga',
 'Abrunheira',
 'Página 2 de 2']

i already done this regex

‘r(?<=sd{7}s)(.*)(?=sw+sd{4}?)’

https://regex101.com/r/uaEc18/1

but i think the problem is d{4}? -> sometimes there are 4 digits, sometimes,not, for exemple in this line
‘72750 7747246 SOLUTO DAKIN SOL 0,5% 1L GSL 1 1 0,60 0,60N 6 0,60’

why the ? (there is or not) isnt working?

enter image description here

The text above is extracted with python from the pdf image. I need to extract the informations in sqares, the blue sqares are done, the yellow i already spent many hours but i can’t get a right regex code, so if you can help i will apreciate a lot

Asked By: foliveir

||

Answers:

You can use

(?<=sd{7}s)(.*)(?=s(?:w+sd{4}|[A-Z]{3,}b))

See this regex demo. Details:

  • (?<=sd{7}s) – a positive lookbehind that matches a location that is immediately preceded with
    • s – a whitespace
    • d{7}s – seven digits and a whitespace
  • (.*) – Group 1: any zero or more chars other than line break chars as many as possible
  • (?=s(?:w+sd{4}|[A-Z]{3,}b)) – a positive lookahead that matches a location that is immediately followed by
    • s – a whitespace
    • (?:w+sd{4}|[A-Z]{3,}b) – either
      • w+sd{4} – one or more word chars, a whitespace, four digits
      • | – or
      • [A-Z]{3,}b – three or more uppercase letters followed with a word boundary.
Answered By: Wiktor Stribiżew