-2

I would like to remove jsessionid=99171C97AE28712E048E321DB6B192F3 from the string below using regex in c#

www.ploscompbiol.org/article/fetchObjectAttachment.action;jsessionid=99171C97AE28712E048E321DB6B192F3?uri=info%3Adoi%2F10.1371%2Fjournal.pone.0066655&representation=XML

I have tried

string id = id.Replace(";jsessionid=.*?(?=\\?|$)", "");

but it is not working, please help!

greedybuddha
  • 7,488
  • 3
  • 36
  • 50
Mobola Oladapo
  • 11
  • 1
  • 1
  • 5
  • That's `string.Replace`, not `Regex.Replace`. Also, there's no way I'm downloading a file from a site I don't know. Post the actual XML. – It'sNotALie. Jun 11 '13 at 17:37
  • I do not thinking posting the actual XML would make any difference. I only need to stop `jsessionid` from appending to the url or remove it. – Mobola Oladapo Jun 11 '13 at 17:43

4 Answers4

1
Regex.Replace(text, "jsessionid=[^\\?]+","")
Tim B
  • 2,340
  • 15
  • 21
0

You can use the following to remove the jsession.

Regex rgx = new Regex("jsessionid=[^\\?]*)");
string id = rgx.Replace(line,"");
greedybuddha
  • 7,488
  • 3
  • 36
  • 50
0

In case you change your mind and you don't want to use RegEx but manage with string.replace function,

        string sample = "ansjsessionid=99171C97AE28712E048E321DB6B192F3wer";
        sample = sample.Replace( sample.Substring(sample.IndexOf("jsessionid="),43),"");

Note that I've assumed that ID is always 32 character long.

Yogee
  • 1,412
  • 14
  • 22
0

Solution not using Regex:

var url = @"www.ploscompbiol.org/article/fetchObjectAttachment.action;jsessionid=99171C97AE28712E048E321DB6B192F3?uri=info%3Adoi%2F10.1371%2Fjournal.pone.0066655&representation=XML";

var startIndex = url.IndexOf("jsessionid=");
var endIndex = url.IndexOf('?', startIndex);
url.Remove(startIndex, endIndex - startIndex);

Note if a '?' is not found then it will throw an exception.

Dustin Kingen
  • 20,677
  • 7
  • 52
  • 92