Reputation: 157
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
Upvotes: 0
Views: 101
Reputation: 7250
This regex does the job:
\(.*\)\/
Just match an opening bracket, then anything until a closing bracket with a forward slash.
Upvotes: 0
Reputation: 856
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))
Upvotes: 0
Reputation: 33566
The key point is to notice that the ()
characters mark the boundaries and that no /
character is in the contents:
/(\(S\([^/()]+\)\))/
Upvotes: 1