简体   繁体   English

需要PHP正则表达式以匹配URL中的模式

[英]Need PHP regex to match pattern in URL

I have problem with matching pattern in URL and getting the right result. 我在URL中匹配模式并获得正确结果时遇到问题。

The URL example: URL示例:

#1: http://www.example.com/webapp/first-text-vs-some-text.html
#2: http://www.example.com/webapp/first-text-a1-vs-sec-b2-text.html
#3: http://www.example.com/webapp/first-text-a1-vs-sec-b2-text-vs-third-c3-text.html
#4: http://www.example.com/webapp/some-text-a1-vs-sec-text-b2-vs-third-text-vs-last-text-c1.html

Result without -vs- (HINT: -vs- can be shown once or repetitive from above URLs): 不带-vs-的结果(提示:-vs-可以显示一次或从上述URL重复显示):

#1: first-text and some-text
#2: first-text-a1 and sec-b2-text
#3: first-text-a1, sec-b2-text and third-c3-text
#4: some-text-1a, sec-text-b2, third-text and last-text-c1

Is there a way of PHP loop to do that? 有没有一种PHP循环方法可以做到这一点?

There is a situation that -vs- in URL structure can be found once or multiple times. 有一种情况,URL结构中的-vs-可以被发现一次或多次。

Should I get my results with few patterns or can it be possible done with one pattern match? 我应该用很少的模式获得结果,还是可以用一种模式匹配来完成?

Should I first split the given URL and then check for characters & symbols left or right of the -vs-? 我应该先分割给定的URL,然后检查-vs-左边还是右边的字符和符号?

Had You have that kind of situation ever? 你曾经有过这种情况吗? Any ideas? 有任何想法吗?

Thanks a lot for help and sharing ideas to get final result! 非常感谢您的帮助和分享想法,以获得最终结果!

You can use the following regex ([^\\/.]+?)(?:-vs-|\\.\\w+$) with preg_match_all to return the different parts before and after -vs- . 您可以将以下正则表达式([^\\/.]+?)(?:-vs-|\\.\\w+$)preg_match_all一起使用,以返回-vs-之前和之后的不同部分。

Regex demo 正则表达式演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM