Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex match but not works in python

I’m a bit confused, a regex pattern works fine but not in python. What am I doing wrong ?
In this works well

Stile = re.compile(r'(^\d{4} (?!Cadempino)[A-Z])([^0-9]*)') 

if Stile.match(line):
    Stile_no = line
    print(Stile_no)
  • 9643 BEIGE EBONY/COCOA 2 6 10 10 10 2 2 42 200.00 8,400.00
  • 1000 NERO 0 0 2 6 10 10 10 2 2 42 218.00 9,156.00
  • 1000 BLACK 0 0 0 2 6 15 15 15 2 2 0 0 57 205.00 11,685.00
  • 1000 BLACK 2 2 2 2 2 1 11 218.00 2,398.00
  • 9769 B.EBONY/NERO 3 3 3 3 3 1 16 244.00 3,904.00

But if i use python i get all strings. my goal is exactly the one shown below

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Looking at your code you probably want to get rid off the matching groups and just print matching group 0:

import re

txt = '''\
6814 Cadempino Door: NUGNES 1920 MENS (TRANI)
411924 KGDHN MBELT W.40 INT. T.GG SUPREME/SELLERIA
9643 BEIGE EBONY/COCOA 2 6 10 10 10 2 2 42 200.00 8,400.00
406831 DJ20N MBELT W.40 GG MAR DOLLAR PIGPRINT
1000 NERO 0 0 2 6 10 10 10 2 2 42 218.00 9,156.00
414516 0YA0G MBELT W.30 GG MAR. PLUTONE CALF
1000 BLACK 0 0 0 2 6 15 15 15 2 2 0 0 57 205.00 11,685.00
406831 0YA0G MBELT W.40 GG MAR PLUTONE CALF
1000 BLACK 2 2 2 2 2 1 11 218.00 2,398.00
627055 92TIN MBELT W.37GG M.R T.GG SUPREME/PLUTONE CALF
9769 B.EBONY/NERO 3 3 3 3 3 1 16 244.00 3,904.00'''

Stile = re.compile(r'^\d{4} (?!Cadempino)[A-Z][^0-9]*')

for line in txt.splitlines():
    m = Stile.match(line)
    if m:
        print(m.group(0))

Prints:

9643 BEIGE EBONY/COCOA 
1000 NERO 
1000 BLACK 
1000 BLACK 
9769 B.EBONY/NERO 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading