I have a string "http://www.something.com/test/?pt=12"
I want to replace pt=12 by pt=13 using regex.
The string after replace will be : "http://www.something.com/test/?pt=13"
How can I achieve this in C#?
I suppose you know the pt= part. I also presume that the param value is a number.
Then, you can use the following regex replacement:
var newval = 13;
var res = Regex.Replace(str, @"\?pt=[0-9]+", string.Format("?pt={0}", newval));
If the param can be non-first in the query string, replace \? with [?&].
Note that you could use the System.UriBuilder class. It has a Query property that you can use to rebuild the query string.
String.Replace()method:myStr.Replace("pt=12", "pt=13").