[英]Get filename without extension from URL with conditional
I've seen lots of examples with getting file names from URL or path, but am unable to solve this problem. 我已经看到了很多从URL或路径获取文件名的示例,但无法解决此问题。
I have a URL that looks like below: 我有一个如下所示的URL:
http://www.bob.net/john/john/ken/mary.html
I need to get Mary
if both /john/john
exist in the URL. 如果URL中同时存在/john/john
我需要得到Mary
。 Otherwise I need to get ken
if only /john
exists in the path 否则,如果路径中仅存在/john
,则需要获取ken
I can get mary
with \\/\\w*\\/\\w*\\/(\\w*)\\/(\\w*)
but that's reliant on Mary
being after the 4th slash. 我可以得到mary
与\\/\\w*\\/\\w*\\/(\\w*)\\/(\\w*)
但是这对依赖Mary
4斜杠之后是。
How can I accomplish this? 我该怎么做?
EDIT - thanks for the responses so far. 编辑-感谢到目前为止的答复。
Any chance of making this dynamic and getting just mary, if /john exists within the url? 如果/ john存在于网址中,是否有可能使它变得动态并变成玛丽? regardless of anything else? 不管别的什么?
To get Mary
if John
is in the path, and John
otherwise, use this: 如果John
在路上,要得到Mary
,否则要得到John
,请使用以下命令:
^(?=.*john).*(mary)|.*(ken)
If there's a match, inspect match[1]
and match[2]
: only one will be set. 如果存在匹配项,请检查match[1]
和match[2]
:仅设置一个。
In Perl and PCRE you can do this a bit more compactly with conditionals or branch reset. 在Perl和PCRE中,可以通过条件或分支重置来更紧凑地执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.