I'm trying to combine 2 different regex with the same names into one and got the error message like this:
Warning: preg_match(): Compilation failed: 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>%'
The other looks like this:
'%<div id="article">[\s\S]*?<h1>(?P<title>[\s\S]*?)</h1>%'
I could combine them in the following way without problem:
'%(<div class="artikkelen">[\s\S]*?<h2>(?P<title>[\s\S]*?)</h2>|<div id="article">[\s\S]*?<h1>(?P<title2>[\s\S]*?)</h1>)%'
But I don't like this way because I use 2 names. I'm wondering whether there is a better way to solve this problem. Thanks in advance.
Best Regards, Box (boxoft...Simple & Great)
I found this page looking for a way to have two (or more?) named subpatterns with the same name, and I found this page with the answer (relevant excerpt here)
Duplicate names
If you are using named patterns you can now have the same name multiple times, provided you put the "(?J)" option in your pattern. For example:
Match: (?J)(?P<name>Ni.+)|(?P<name>Fr.+)
This would match "Nick" or "Fred", and whichever one matched, the named pattern "name" would be set to it.
just contributing knowledge back to the universe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With