Question

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!

Was it helpful?

Solution

Regex.Replace(text, "jsessionid=[^\\?]+","")

OTHER TIPS

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.

You can use the following to remove the jsession.

Regex rgx = new Regex("jsessionid=[^\\?]*)");
string id = rgx.Replace(line,"");

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top