gpt4 book ai didi

php - 将 2 个正则表达式合并为一个时得到 "two named subpatterns have the same name"

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:51:38 24 4
gpt4 key购买 nike

我试图将 2 个具有相同名称的不同正则表达式合并为一个,但得到的错误消息如下:

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

一个正则表达式如下所示:

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

另一个看起来像这样:

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

我可以毫无问题地按以下方式组合它们:

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

但我不喜欢这种方式,因为我使用了 2 个名字。我想知道是否有更好的方法来解决这个问题。提前致谢。

最好的问候,盒子(boxoft...简单而伟大)

最佳答案

我发现这个页面正在寻找一种方法来拥有两个(或更多?)同名的命名子模式,我找到了 this page与答案(相关摘录在这里)

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.

只是将知识回馈给宇宙

关于php - 将 2 个正则表达式合并为一个时得到 "two named subpatterns have the same name",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1420626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com