简体   繁体   English

RegEx匹配URL中第一个反斜杠后的所有内容

[英]RegEx to match everything after first backslash in URL

I need help trying to figure out the regex to match only what is after the first backslash in a url. 我需要帮助尝试找出正则表达式以仅匹配网址中第一个反斜杠之后的内容。

URL Example: http://www.TestThis.com/Some-Data/Some-Other-Data/Even-More-Data URL示例: http://www.TestThis.com/Some-Data/Some-Other-Data/Even-More-Datahttp://www.TestThis.com/Some-Data/Some-Other-Data/Even-More-Data

Pattern: ^(.*?//.*?/)(.*)$ 模式: ^(.*?//.*?/)(.*)$

I get 2 matches, which can work, but I really only want what is after the http://www.TestThis.com/ 我得到了2个匹配项,这些匹配项可以正常工作,但是我真的只想要http://www.TestThis.com/之后的内容http://www.TestThis.com/

Uri.PathAndQuery is better approach than any regEx you can come up with for valid Urls. Uri.PathAndQuery比有效的Urls可以提供的任何regEx更好的方法。 It will also handle cases of IP instead of DNS name, % encoding used in host portion, ports and like. 它还将处理IP而不是DNS名称,主机部分,端口等中使用的%编码的情况。

 Console.Write(
    new Uri(
     "http://www.TestThis.com/Some-Data/Some-Other-Data/Even-More-Dat"
    ).PathAndQuery);

You could try it without regex: 您可以尝试不使用正则表达式的方法:

string test = "http://www.TestThis.com/Some-Data/Some-Other-Data/Even-More-Data";
string intermediate = test.Split(new string[] { "//" }, StringSplitOptions.RemoveEmptyEntries)[1];
string final = intermediate.Substring(intermediate.IndexOf('/') + 1);

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

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