开发者

Regular expression to convert url parameter string into path

开发者 https://www.devze.com 2022-12-13 12:48 出处:网络
Need regular expression to change below url abc.aspx?str=blue+lagoon&id=1234 to /blog/blue-lagoon/ 开发者_JAVA技巧in perl:

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

精彩评论

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