需要获得如此子字符串但使用正则表达式

时间:2016-07-12 10:43:29

标签: c# asp.net regex

我的网址就像。

http://EddyFox.com/x/xynua

需要在/x/之后获取子字符串。

我面临的复杂例子是:

http://EddyFox.com/x//x/

此结果应为/x/

可以使用substring实现,但我们需要使用正则表达式来执行它。

3 个答案:

答案 0 :(得分:1)

这应该这样做:

string s = "http://EddyFox.com/x/xynua";
// I guess you don't want the /x/ in your match ?=!
Console.WriteLine(Regex.Match(s, "/x/(.*)").Groups[1].Value );

这可能更好:

Console.WriteLine(Regex.Match(s, "(?<=/x/)(.*)").Value );

输出

  

xynua

看一下这篇文章:Regex to match after specific characters这里有很多RegEx帖子。之前已经提出过RegEx问题的概率非常高。 :)

答案 1 :(得分:0)

正则表达式/x/(.*)将捕获/x/

之后的所有内容

答案 2 :(得分:0)

问题出在哪里?

var r = new Regex("/x/(\\S*)");
var matches = r.Matches(myUrl);

此正则表达式匹配从/x/到第一次出现白色空间的所有内容。