preg_match 匹配可选字符串,但不匹配所有字符串

发布于 2024-08-26 16:54:59 字数 1480 浏览 7 评论 0原文

以以下正则表达式匹配为例。

preg_match('!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-[0-9]+)?$!', 'publisher/news/1/2010-march:03-23/test_title/1/page-1', $matches); 
print_r($matches);

它产生以下内容:

Array
(
    [0] => publisher/news/1/2010-march:03-23/test_title/1/page-1
    [1] => news
    [2] => 1
    [3] => 2010
    [4] => march
    [5] => 03
    [6] => 23
    [7] => test_title
    [8] => 1
    [9] => /page-1
)

但是,由于最后一个匹配是可选的,它也可以匹配以下“publisher/news/1/2010-march:03-23/test_title/1”。我的问题是,我希望能够匹配 (/page-[0-9]+) 如果存在,但仅匹配页码,因此“publisher/news/1/2010-march:03-23/test_title/ 1/page-1" 会像这样匹配:

Array
(
    [0] => publisher/news/1/2010-march:03-23/test_title/1/page-1
    [1] => news
    [2] => 1
    [3] => 2010
    [4] => march
    [5] => 03
    [6] => 23
    [7] => test_title
    [8] => 1
    [9] => 1
)

我尝试了以下正则表达式

'!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)/?p?a?g?e?-?([0-9]+)?$!'

这有效,但它也会匹配“publisher/news/1/2010-march:03-23/test_title/1/1”。我不知道要进行一场比赛,但在比赛中却没有表现出来?在单个正则表达式中可能吗?

Take for example the following regex match.

preg_match('!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-[0-9]+)?$!', 'publisher/news/1/2010-march:03-23/test_title/1/page-1', $matches); 
print_r($matches);

It produces the following:

Array
(
    [0] => publisher/news/1/2010-march:03-23/test_title/1/page-1
    [1] => news
    [2] => 1
    [3] => 2010
    [4] => march
    [5] => 03
    [6] => 23
    [7] => test_title
    [8] => 1
    [9] => /page-1
)

However as the last match is optional it can also work with matching the following "publisher/news/1/2010-march:03-23/test_title/1". My problem is that I want to be able to match (/page-[0-9]+) if it exists, but match only the page number so "publisher/news/1/2010-march:03-23/test_title/1/page-1" would match like so:

Array
(
    [0] => publisher/news/1/2010-march:03-23/test_title/1/page-1
    [1] => news
    [2] => 1
    [3] => 2010
    [4] => march
    [5] => 03
    [6] => 23
    [7] => test_title
    [8] => 1
    [9] => 1
)

I've tried the following regex

'!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)/?p?a?g?e?-?([0-9]+)?$!'

This works, however it will also match "publisher/news/1/2010-march:03-23/test_title/1/1". I have no idea to perform a match but not have it come back in the matches? Is it possible in a single regex?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Smile简单爱 2024-09-02 16:54:59

绝对不匹配 publisher/news/1/2010-march:03-23/test_title/1/whatever

!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(?:/page-([0-9]+))?$!

仍匹配 publisher/news/1/2010-march:03-23/ test_title/1/whatever 但忽略 /whatever

!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(?:(?:/page-([0-9]+))|/.*)?$!

To absolutely not match publisher/news/1/2010-march:03-23/test_title/1/whatever

!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(?:/page-([0-9]+))?$!

To still match publisher/news/1/2010-march:03-23/test_title/1/whatever but ignore the /whatever:

!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(?:(?:/page-([0-9]+))|/.*)?$!
白云悠悠 2024-09-02 16:54:59

也许是这样的:

'!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-([0-9]+))?$!'

maybe like that:

'!^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)(/page-([0-9]+))?$!'
活雷疯 2024-09-02 16:54:59

这就是您正在寻找的正则表达式:

^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)/(?:page-(\d+))?

您可以在 rexexbuddy 中测试它。如果“page-1”未设置,则 var 9 将为空,否则将设置它。

This is the regex what you are looking for:

^publisher/([A-Za-z0-9\-\_]+)/([0-9]+)/([0-9]{4})-(january|february|march|april|may|june|july|august|september|october|november|december):([0-9]{1,2})-([0-9]{1,2})/([A-Za-z0-9\-\_]+)/([0-9]+)/(?:page-(\d+))?

You can test it in rexexbuddy. If "page-1" is not set it will leave var 9 empty else it will set it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文