Got "two subpattern names have the same name" when combining 2 regular expressions into one

I am trying to combine 2 different regular expressions with the same name into one and get an error message as follows:

Warning: preg_match (): Compilation failed: the two named subpatterns have the same name at offset 276 ...

One regex looks like this:

'%<div class="artikkelen">[\s\S]*?<h2>(?P<title>[\s\S]*?)</h2>%'

Another is as follows:

'%<div id="article">[\s\S]*?<h1>(?P<title>[\s\S]*?)</h1>%'

I could combine them as follows without problems:

'%(<div class="artikkelen">[\s\S]*?<h2>(?P<title>[\s\S]*?)</h2>|<div id="article">[\s\S]*?<h1>(?P<title2>[\s\S]*?)</h1>)%'

But I do not like it because I use 2 names. I am wondering if there is a better way to solve this problem. Thanks in advance.

Best regards, Box (boxoft ... Simple and excellent)

+3
source share
2 answers

:

%<div (class="artikkelen"|id="article")>[\s\S]*?<h[12]>(?P<title>[\s\S]*?)</h[12]>%

, , .

0

, ( ?) , ( )

, , "(? J)" . :

    Match: (?J)(?P<name>Ni.+)|(?P<name>Fr.+)

"" "", , , "" .

+24

Source: https://habr.com/ru/post/1717625/


All Articles