简体   繁体   中英

How to replace a specific character in url with a “” only after the?

I want to replace the & from the url which come after ? ie query string.

Example
http://stackoverflow.com&/questions/ask?a=abcd&b=bcd&-------->1

Suppose there is a Url as 1 Now I want to replace the amp; with ""

http://stackoverflow.com&/questions/ask?a=abcd&b=bcd&-------->2

I have tried this regex as

\?(.*?&(amp;).*?){1,} for using Regex.Replace(......);

I want to replace group[2] with "". How to do that??????????

You can use this:

string pattern = @"((?:\?|\G(?<!^))(?:[^&]+|&(?!amp;))*&)amp;";
string replacement = "$1";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(input, replacement);

Pattern details:

(                         # capturing group: content after ? and before &amp; 
    (?:                   # non capturing group: entry point
        \?                # literal ?
      |                   # OR
        \G(?<!^)          # a match contiguous to a precedent match
    )
    (?:[^&]+|&(?!amp;))*  # all that is not a & or a & not followed by "amp;"
&                         # literal & before "amp;"
)
amp;

You probably don't want to replace amp; by nothing, but &amp; by &.

I suppose Regex is not the correct thing to use here. See Get url parameters from a string in .NET on how to extract parameters from a URL. This will automatically convert &amp; to &

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM