Python regex 2 or statments in 1 string

Question:

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.

Asked By: Harun MISTIK

||

Answers:

Try (Regex demo):

import re

def kanunMaddesi(madde):
    r1 = re.compile(r"bd{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']
Answered By: Andrej Kesely
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.