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
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\([^/()]+\)\))/
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))
This regex does the job:
\(.*\)\/
Just match an opening bracket, then anything until a closing bracket with a forward slash.