regex - 正则表达式 : Matching text up to last index of character

标签 regex http

例如:

http://foobar.com/foo/bar/foobar.php

从这个地址,我需要提取以下内容:

http://foobar.com/foo/bar

我已经尝试使用以下正则表达式:

(?<namespace>.*)/.*?

但是返回值是

http:

有人可以帮忙吗?谢谢。

最佳答案

试试这个:

^(?<namespace>.*)/[^/]+$

快速解释:

^                    # the start of input
(?<namespace>.*)/    # zero or more chars followed by a '/' (which the last '/') 
[^/]+                # one or more chars other than '/'
$                    # the end of input

关于regex - 正则表达式 : Matching text up to last index of character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7262547/

相关文章:

javascript - 用引号替换双引号

c# - 拆分具有键值对的文本行,其中值可以为空

java - 将两种方法合二为一

apache-flex - 如何实现 HTTP 隧道

java - 如何更改 java.net.http.HttpClient 的用户代理字符串

security - 在 HEADER 或 URL 中传递 API key ?

http - 如何判断文件是否已通过 HTTP 完全下载?

javascript - 仅替换函数第一次出现的情况

java - 如何修复正则表达式以剥离 <blockquote></blockquote> 标签,其中包含\n ?

c# - 阿拉伯语正则表达式