Regex.Replace(text, "jsessionid=[^\\?]+","")
Using regex function to manipulate string in c#
-
31-05-2022 - |
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!
La solution
Autres conseils
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.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow