-6

i have a string suppose that one

  http://www.whitelabelhosting.co.uk/flight-search.php?dept=any&journey=R&DepTime=0900 

and now what i am doing is here in c sharp

       string linkmain = link.Replace("&DepTime=", "&DepTime=" + journey);

but the time is being added as 09000900

and in case of

   string linkmain =   link.Replace("Journey=", "Journey="+journey);

journey added as RR

so i have to get the value of R that is after Journer=? AND deptTime=?

that are not same every time so how to get them during replace as they are present just after where ? sign is marked

this is a post operation so parameter are different like

journey :

R

M

O

and time :

0900 , 1200 , 0400

GLOBAL TECH
  • 71
  • 10
  • I have edited your question so it can actually be understood. – CodeCaster Mar 10 '16 at 16:23
  • no you donot understand actually `Journey=` is same in string every time but its next value that is `R` will be different so we have to replace `R` but we donot actually now is this R or O or M depends upon journey so we now its value that is `JOURNEY=??` – GLOBAL TECH Mar 10 '16 at 16:27
  • 1
    It doesn't matter which query string parameter your question is about, the principle remains the same. Do you understand your current question is very unreadable and hard to understand, and that my edit made it actually answerable? Is it a possibility that you don't know what you're doing, and that you don't understand the edit? – CodeCaster Mar 10 '16 at 16:28
  • 1
    There are built in classes to handle query strings, doing this with `RegEx` is just making this harder than it has to be. [This question may help you.](http://stackoverflow.com/questions/829080/how-to-build-a-query-string-for-a-url-in-c) – Michael McGriff Mar 10 '16 at 16:59
  • Wait so you expected to take a string like 'www.website.com?foo=bar' and replace 'foo=' with something and it update 'bar' using some magic? – Hugo Yates Mar 11 '16 at 09:24

2 Answers2

2

Use HttpUtility.ParseQueryString(url) with will return a NameValueCollection. You can then loop this collection to manipulate the data how you want and build the new url from that.

https://msdn.microsoft.com/en-us/library/ms150046(v=vs.110).aspx

James Dev
  • 2,979
  • 1
  • 11
  • 16
0

You may want to try out the following regex. Regex101 link

((?:\?.*?&|\?)journey=)[^&]*

Try out the following code to replace the value of journey to replacement

string url = "http://www.whitelabelhosting.co.uk/flight-search.php?dept=any&journey=R&DepTime=0900";
string newUrl = Regex.Replace(url, @"((?:\?.*?&|\?)journey=)[^&]*", "$1"+"replacement");

Remember to add the following to your file:

using System.Text.RegularExpressions;

You can do the same for DepTime using the following regex:

((?:\?.*?&|\?)DepTime=)[^&]*
Harsh Poddar
  • 2,394
  • 18
  • 17