Click here to Skip to main content
15,906,333 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,
I would like to match a string between || in a string.
Here is pat of the code

match = ">ipi|IPI00831484| IPI00832484.1 Uncharacterized etc"
string match = stream.Readline();

I would like to match the between the || e.g. the IPI00831484
with

match = Regex.Match(match, "IPI00831484");

This doesn't work. Can anyone direct me to what is worng or any alternative solution?
Thanks,
Shekee
Posted

No need for a Regular Expression here. Use String.Split:

String match = ">ipi|IPI00831484| IPI00832484.1 Uncharacterized etc"
String[] values = match.Split(new char[] {'|'});
//values now contains three string 
String yours = values[1]; 


Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 22:31pm    
Yes, why indeed? As it comes to finding anything in a string, always Regex...
My 5.
--SA
If you prefer regular expressions then try this:

C#
Regex rx = new RegEx("|[^|]*|");
Match match = rx.Match(">ipi|IPI00831484| IPI00832484.1 Uncharacterized etc");
if(match.Succeess)
{
    Console.WriteLine(match.Value);
}


Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Feb-11 22:32pm    
You could not stand it! (Not a bid deal though...), my 5.
--SA
Sergey Alexandrovich Kryukov 11-Feb-11 22:43pm    
However, such questions make little sense, why not pushing OP to learn a bit?
I tried, please see my Answer.
--SA
Your attempted Regex sample shows that you did not even try to read on how to use Regular Exception. Why doing guess work?

Your idea of asking your question on CodeProject was not so reasonable either. You ask one, two time, now what. This is pretty much like asking "How much is 2*2", and later "How much is 11*3?", don't you think so. At the same time there is more then enough literature and references (you cannot remember it all, will need a reference).

Read this: http://en.wikipedia.org/wiki/Regex[^], http://msdn.microsoft.com/en-us/library/hs600312.aspx[^], use http://cstheory.stackexchange.com/questions/1854/is-finding-the-minimum-regular-expression-an-np-complete-problem[^], for reference: http://www.regular-expressions.info/reference.html[^] and http://www.regular-expressions.info/refadv.html[^].

Good luck,
—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900