I'm new to regular expressions, and have no clue where to start, it's like a diff language to me. But I need one quick to accomplish a task.
我是正则表达式的新手,并且不知道从哪里开始,这对我来说就像是一种差异语言。但我需要一个快速完成任务。
I need to take
我需要服用
http://www.domain.com/folder1/folder2/file_path.txt
and get just
得到的
/folder1/folder2/file_path.txt
from it.
Thanks!
4 个解决方案
#1
construct a URI object from it and one of the properties of it will have what you want.
从它构造一个URI对象,其中一个属性将具有你想要的。
#2
I think that regex should work:
我认为正则表达式应该有效:
^http://.*?/(.*)$
(tested with Python)
(用Python测试)
#3
Since VB.NET is in the tag for this question, I assume you have access at the server side to the Request object:
由于VB.NET位于此问题的标记中,因此我假设您可以在服务器端访问Request对象:
Dim instance As HttpRequest
Dim value As String
value = instance.Path
This should give you exactly what you asked for.
这应该会给你你所要求的。
Edit: On second thought - you could be parsing URLs from some input string... in which case, regex will only help if you have a simple (regular) set of inputs:
编辑:第二个想法 - 你可以从一些输入字符串解析URL ...在这种情况下,正则表达式只有在你有一组简单(常规)输入时才有用:
Do you know all the possible domains? i.e. are "http://www.ABC.com" and "http://www.DEF.com" the only possible domains?
你知道所有可能的域吗?即“http://www.ABC.com”和“http://www.DEF.com”是唯一可能的域名?
Then here:
Dim text As String = "http://www.ABC.com/folder1/folder2/file.txt"
Dim pattern As String = "(?:http://www.ABC.com|http://www.DEF.com)(.*)"
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim g as Group = m.Groups(2) 'Gives the string matched by the capturing parentheses
#4
Supporting more protocols and making the protocol optional too.
支持更多协议并使协议也可选。
((https?|ftp)://)?(.*?)/(.*)
#1
construct a URI object from it and one of the properties of it will have what you want.
从它构造一个URI对象,其中一个属性将具有你想要的。
#2
I think that regex should work:
我认为正则表达式应该有效:
^http://.*?/(.*)$
(tested with Python)
(用Python测试)
#3
Since VB.NET is in the tag for this question, I assume you have access at the server side to the Request object:
由于VB.NET位于此问题的标记中,因此我假设您可以在服务器端访问Request对象:
Dim instance As HttpRequest
Dim value As String
value = instance.Path
This should give you exactly what you asked for.
这应该会给你你所要求的。
Edit: On second thought - you could be parsing URLs from some input string... in which case, regex will only help if you have a simple (regular) set of inputs:
编辑:第二个想法 - 你可以从一些输入字符串解析URL ...在这种情况下,正则表达式只有在你有一组简单(常规)输入时才有用:
Do you know all the possible domains? i.e. are "http://www.ABC.com" and "http://www.DEF.com" the only possible domains?
你知道所有可能的域吗?即“http://www.ABC.com”和“http://www.DEF.com”是唯一可能的域名?
Then here:
Dim text As String = "http://www.ABC.com/folder1/folder2/file.txt"
Dim pattern As String = "(?:http://www.ABC.com|http://www.DEF.com)(.*)"
Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)
' Match the regular expression pattern against a text string.
Dim m As Match = r.Match(text)
Dim g as Group = m.Groups(2) 'Gives the string matched by the capturing parentheses
#4
Supporting more protocols and making the protocol optional too.
支持更多协议并使协议也可选。
((https?|ftp)://)?(.*?)/(.*)