tags:

views:

237

answers:

4

Hello guys,

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,

A: 

This regex will put "my-page-title" in the second group:

^([^/]*/){2}([^/]*)/$

If you always want the last group you can use:

^.*/([^/]*)/$
bah, the formatting hid my *
^.*/ isn't necessary in the second regex.
statenjason
+1  A: 

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

.+/(.+)/?
jitter
A: 

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

Frozzare
you need a + after [^/] otherwise it would only capture titles that are one character long, which is why you're getting null.
statenjason
A: 

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];
statenjason