开发者

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 + "/";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜