Márton Barta
Márton Barta

Reputation: 33

Combining 2 regular expressions

I have 2 strings and I would like to get a result that gives me everything before the first '\n\n'.

'1. melléklet a 37/2018. (XI. 13.) MNB rendelethez\n\nÁltalános kitöltési előírások\nI.\nA felügyeleti jelentésre vonatkozó általános szabályok\n\n1.

'12. melléklet a 40/2018. (XI. 14.) MNB rendelethez\n\nÁltalános kitöltési előírások\n\nKapcsolódó jogszabályok\naz Önkéntes Kölcsönös Biztosító Pénztárakról szóló 1993. évi XCVI. törvény (a továbbiakban: Öpt.);\na személyi jövedelemadóról szóló 1995. évi CXVII.

I have been trying to combine 2 regular expressions to solve my problem; however, I could be on a bad track either. Maybe a function could be easier, I do not know.

I am attaching one that says that I am finding the character 'z'

extended regex : [\z+$]
I guess finding the first number is: [^0-9.].+

My problem is how to combine these two expressions to get the string inbetween them?

Is there a more efficient way to do?

Upvotes: 1

Views: 36

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626870

You may use

re.findall(r'^(\d.*?)(?:\n\n|$)', s, re.S)

Or with re.search, since it seems that only one match is expected:

m = re.search(r'^(\d.*?)(?:\n\n|$)', s, re.S)
if m:
    print(m.group(1))

See the Python demo.

Pattern details

  • ^ - start of a string
  • (\d.*?) - Capturing group 1: a digit and then any 0+ chars, as few as possible
  • (?:\n\n|$) - a non-capturing group matching either two newlines or end of string.

See the regex graph:

enter image description here

Upvotes: 1

Related Questions