简体   繁体   English

PHP正则表达式最后出现的单词

[英]PHP regex last occurrence of words

My string is: /var/www/domain.com/public_html/foo/bar/folder/another/.. 我的字符串是: /var/www/domain.com/public_html/foo/bar/folder/another/..

I want to remove the root folder from this string, to get only public folder, because some servers have multiple websites inside. 我想从这个字符串中删除根文件夹,只获取公共文件夹,因为有些服务器里面有多个网站。

My actual regex is: /^(.*?)(www|public_html|public|html)/s 我的实际正则表达式是:/ /^(.*?)(www|public_html|public|html)/s )/ /^(.*?)(www|public_html|public|html)/s

My actual result is: /domain.com/public_html/foo/bar/folder/another/.. 我的实际结果是: /domain.com/public_html/foo/bar/folder/another/..

But i want to remove the last ocorrence, and get somethig like this: /foo/bar/folder/another/.. 但我想删除最后一个ocorrence,并获得像这样的东西: /foo/bar/folder/another/..

Thanks! 谢谢!

You have to use a greedy quantifier and to check if the alternative is enclosed between slashes using lookarounds: 你必须使用一个贪婪的量词,并检查替代是否使用lookarounds包含在斜杠之间:

/^.*(?<![^\/])(?:www|public(?:_html)?|html)(?![^\/])/

About the lookarounds: I use negative lookarounds with a negated character class to check if there is a slash or the limit of the string at the same time. 关于外观:我使用带有否定字符类的负面外观来检查字符串是否同时存在斜杠或限制。 This way you are sure that for instance html is a folder and not the part of another folder name. 这样你可以肯定,例如html是一个文件夹,而不是另一个文件夹名称的一部分。

I removed the s modifier that is useless. 我删除了无用的修饰符。 I removed the capture groups too since the goal is to replace all with an empty string. 我也删除了捕获组,因为目标是用空字符串替换all。

The ? ? makes your expression non-greedy which is not actually what you want here. 使你的表达非贪婪,这实际上不是你想要的。 Try: 尝试:

^(.*)(www|public_html|public|html)

which should keep going until the last match. 这应该一直持续到最后一场比赛。

Demo: https://regex101.com/r/v5WbB3/1/ 演示: https//regex101.com/r/v5WbB3/1/

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

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