0

I am looking for a regex to match some part of URL to redirect my pages accordingly.

I have googled it with no luck.

EX:

http://www.somesite.com/cat1/cat2/

In this i am looking to match any word or char after the domain name, so it would be /cat1/cat2/

http://www.somesite.com/cat1/cat2/cat3/

In this i am looking to match any word or char after the domain name, so it would be /cat1/cat2/cat3/


looking for 2 diff regex to match this kind of url. First regex that matches only 2 category and second regex that matches only 3 category.

Thanks guys in advance.

1
  • C#. I am already having a rewrite rule config file for my application. so i am not looking for any solution other then REGEX.. my company would not allow me to use other solution. thanks... Commented Oct 4, 2012 at 9:55

4 Answers 4

2

This will work for the first case, with the desired part in the first capturing group:

^http://[^/]+(/(?:[^/]*/){2})$

Change the {2} part to {3} for the second case.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks for your answer. But when i am checking online your regex does not match any url. i might be wrong. this is the site i used regexpal.com
Try it now. I initially assumed that you were using JavaScript, so I escaped the forward slashes. That may not be how it works in C#.
Sorry for the late reply.. actually i do not want to match the whole URL..just need to match /cat1/cat2/ and /cat1/cat2/cat3/ from URL..can you please revise your regex and let me know ...thanks
The first capturing group contains the part you want. You can access it using myMatch.Groups[1], where myMatch is the Match object (I think; I don't use C#, myself).
1

Sounds pretty simple.

First regex would be like : http://.+/([^/]+)/([^/]+)/

I'll let you guess what the second regex is.

1 Comment

your regex is matching both somesite.com/cat1/cat2 and also somesite.com/cat1/cat2/cat3
1

As far as I know, regexs do not support inverse matching. That means, you cannot write a regex that matches what is NOT in the regex (if I'm wrong, please someone correct me on this). That being said, you could use this:

^http:\/\/[^\/]+\/(.*)

Using grouping, you can match whatever comes after your URL domain. So, in this case, what you are looking for would be fetcheable through $1 (the group represented by the brackets at the end of the regex). Another important thing is the ^ in the beginning of the regex. This way you won't catch URL strings passed as query parameters of your URL.

3 Comments

by the way, this will catch everything, including the query part of the url. if you only want to get the relative path, then you'll have to complete it for the case you catch a '?' character.
Sorry for the late reply.. actually i do not want to match the whole URL..just need to match /cat1/cat2/ and /cat1/cat2/cat3/ from URL..can you please revise your regex and let me know ...thanks
ok, so what you want is the segment. ^http:\/\/[^\/]+\/([^?]*) will do.
0

you can get path name from windwo.location.pathname

var path = window.location.pathname

after getting this path you can get path using split('/');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.