在符号之间获取文本,正则表达式?
我有一个问题,我无法找到一种方法来获取符号之间的文本。
site.com/hello-world/my-page-title/
我想获得我的页面标题? 怎么样? 谢谢你的帮助,
这个正则表达式将“my-page-title”放在第二组中:
^([^/]*/){2}([^/]*)/$
如果您总是想要最后一组,您可以使用:
^.*/([^/]*)/$
只要URI以斜杠终止,此正则表达式始终为您提供第一个捕获组中的最后一个URI段
.+/(.+)/
如果斜线有时会错过,你可以使用
.+/(.+)/?
好吧,我对正则表达式不是很好,但标题返回null?
string url = /hello-world/my-page-text/ string title = Regex.Match(url, @"^*./([^/])/$").Groups[1].Value;
它确实有效,*是正则表达式代码中的错误
你可以使用正则表达式或String.Split
//Regex string s = "site.com/hello-world/my-page-title/"; Match match = Regex.Match(s, "([^/]+)/$"); string matchedString = match.Groups[1].Value; //Split string[] sections = s.Split(new char[]{'/'},StringSplitOptions.RemoveEmptyEntries); string lastSection = sections[sections.Length - 1];