Home > OS >  Python regex 2 or statments in 1 string
Python regex 2 or statments in 1 string

Time:12-31

I'm trying to get law article from string, but my regex is splitting at every law article.

def kanunMaddesi(madde):
    r1 = re.compile("[0-9]{3}\/[0-9]{1,2}|[-|\/|.|_| ][a-zA-Z]{1}")
    return r1.findall(madde)

madde = (kanunMaddesi("5237SA 116/1 119/1 C 58/6 53/1 58 58/7."))

Python result in list: ['116/1', '119/1', ' C']

I expected: ['116/1', '119/1 C']

String = 5237SA 116/1 119/1 C 58/6 53/1 58 58/7.

I've tried this regular expression

[0-9]{3}\/[0-9]{1,2}|[-|\/|.|_| ][a-zA-Z]{1}

This regular expression result is: 5237SA (116/1) (119/1) (C) 58/6 53/1 58 58/7.

My expected result: 5237SA (116/1) (119/1 C) 58/6 53/1 58 58/7.

CodePudding user response:

Try (Regex demo):

import re

def kanunMaddesi(madde):
    r1 = re.compile(r"\b\d{3}\/\d{1,2}(?:[\/._ -][a-zA-Z]{1})?\b")
    return r1.findall(madde)

madde = kanunMaddesi("5237SA 116/1 119/1 C 58/6 53/1 58 58/7.")
print(madde)

Prints:

['116/1', '119/1 C']
  • Related