Regular expression to convert url parameter string into path
Need regular expression to change below url
abc.aspx?str=blue+lagoon&id=1234
to
/blog/blue-lagoon/
开发者_JAVA技巧
in perl:
my $work_url = $original_url;
$work_url =~ s/\+/-/g;
$url = '/blog/' . do { $work_url =~ m/\bstr=([\w\-]+)\b/; $1} . '/';
works for the example given.
inspired by Ragepotato:
$new_url = '/blog/'
. sub { local $_ = shift; tr/+/-/; m/\bstr=([\w\-]+)\b/; $1 }->($orig_url)
. '/';
And an stricter, less greedy regex for Ragepotatos post, untested:
Regex.Match(input.Replace("+", "-"),@"\bstr=(.*?)&").Groups[1].Value
C# .NET
string input = "abc.aspx?str=blue+lagoon&id=1234";
string output = "/blogs/" + Regex.Match(input.Replace("+", "-"),@"str=(.*)&").Groups[1].Value + "/";
精彩评论