Against a string like this:
<h3>title</h3>
<h4>title</h4>
How to match the tags correspondingly and get the text in them?
This works but it unnecessarily gets the tag name:
'@<(h[34])>(.+)</\1>@sU'
However this doesn't seem to work as I don't want to get the tag name but just want to backreference it:
'@<(?:h[34])>(.+)</\1>@sU'
I'm using PHP preg_match(). Why doesn't the 2nd approach work? Is it possible to back reference a non-capturing group?
Capturing groups could be used later on in the regular expression as a backreference to what was matched in that captured group. By placing ?:
inside you specify that the group is not to be captured, but to group expressions.
You can use the branch reset feature (?| ... | ... )
that way you don't have your expression matching non-corresponding tags and both capturing groups in the alternatives are considered as one capturing group.
~(?|<h3>(.+?)</h3>|<h4>(.+?)</h4>)~s
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.