2

i want to find category from the link. I am breaking the link into array using Regex like this.

string input = link; string pattern = "/"; // Split on hyphens string[] substrings = Regex.Split(input, pattern); foreach (string match in substrings) { Console.WriteLine("'{0}'", match); } 

My links are:

http://www.example.com/lifestyle/food/a93ypt9-1227277841603?from=public_atom http://www.example.com/sports/scoccer/accept9-1227277841603?from=public_atom 

Whereas categories of the above links

lifestyle sports 

I am splitting the link on hyphens and finding out my category but is there any better way to accomplish my task?

2 Answers 2

6

Why do you think you need to use regular expressions? The Uri class can parse and interpret the paths for you. You can get direct access to the Segments.

var uri = new Uri("http://www.link.com/lifestyle/food/a93ypt9-1227277841603?from=public_atom"); uri.Segments; // [ "/", "lifestyle/", "food/", "a93ypt9-1227277841603" ] 
Sign up to request clarification or add additional context in comments.

1 Comment

That's why he posted, he was looking for something other than regular expressions.
1

If you need Regex pattern (exactly for your links), try this:

\/{1,2} 

or

\/{1,2}|\?{1} 

for splitting parametres too

see .net regex tester

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.