I have a url:
http://example.com/(S(4txk2wasxh3u0slptzi20qyj))/CWC_Link.aspx
but I only want to extract this portion:
(S(4txk2anwasxh3u0slptzi20qyj))/
Please, can anyone suggest me regex for this
The key point is to notice that the () characters mark the boundaries and that no / character is in the contents:
/(\(S\([^/()]+\)\))/
/(S(4txk2anwasxh3u0slptzi20qyj))/.(S(4txk2anwasxh3u0slptzi20qyj))), not $0 (whole match /(S(4txk2anwasxh3u0slptzi20qyj))/). Without that bounding characters, if you pass an url of http://farmer.gov.in/asdada(S(foo))asdasd/(S(key))/asdasdasd you might catch the 'foo' instead of the 'key'. But of course that's anyways so improbable that you can probably safely remove the extra bounding '/'s.Also you won't catch the foo if you restrict your regex to match for a forward slash - this is exactly why I included a '/' at both sides. Compare that to your regex that is capable of capturing many more false positives. As to the tail, I've completely intentionally left the trailing '/' off the regex, because I take it as a typo on the OP side, because he clearly wanted to catch the 'magic string' from the URL. He didn't complain mind you.Here's your regex. The part in braces will extract needed fragment
/^.+\/([^\/]+)\/.+$/
Basically, the logic is simple:
^ - marks beginning of the string
.+\/ - matches all symbols before the next part. This part of regex is composed taking into account default "greedy" behaviour of regexes, so this part matches http://farmer.gov.in/ in your example
([^\/]+) - matches all symbols between two slashes
\/.+$ - matches all symbols till the end of the string
Example with PHP language:
<?php
$string = "http://farmer.gov.in/(S(4txk2wasxh3u0slptzi20qyj))/CWC_Link.aspx";
$regex = "/^.+\/([^\/]+)\/.+$/";
preg_match($regex, $string, $matches);
var_dump($matches);
?>
In the output $matches[1] will have your needed value (S(4txk2wasxh3u0slptzi20qyj))
parse_urlfunction. Perl:URImodule. Ruby:URImodule. .NET: 'Uri' class