I have a problem, i can’t figure out a way to get out text between symbols.
site.com/hello-world/my-page-title/
i want to get my-page-title only? How?
Thanks for your help,
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
This regex always gives you the last URI segment in the first capturing group as long as the URI is terminated with a slash
.+/(.+)/
if the slash sometimes misses you can use
.+/(.+)/?
Method 2
This regex will put “my-page-title” in the second group:
^([^/]*/){2}([^/]*)/$
If you always want the last group you can use:
^.*/([^/]*)/$
Method 3
Well, i am not so good with regex, but title return null?
string url = /hello-world/my-page-text/ string title = Regex.Match(url, @"^*./([^/])/$").Groups[1].Value;
it did work, the * was the error in the regex code
Method 4
You could use regex or 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];
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0