Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a string like this Stock[Oid,Part.Decription]
How can i get
firstpart:Stock
secondpart: Oid,Part.Decription with regex. "," can be more
Posted

Try:
^(?<Stock>.+)\[(?<Oid>.+)[,](?<Part>.+)\.(?<Description>.+)\]$
If the comma can be a variety of chars, add them between the '[' and ']'.
For example, if it could be comma, semicolon, or colon:
^(?<Stock>.+)\[(?<Oid>.+)[,;:](?<Part>.+)\.(?<Description>.+)\]$
 
Share this answer
 
Comments
Oguz Türkan 19-Dec-14 9:29am    
thanks ^(?<stock>.+)\[(?<oid>.+)]$ worked for me
check with below:

C#
string pattern = "(\\w+)\\[([^\\]]+)\\]";
string1 = "Stock[Oid,Part.Decription]";
MatchCollection matches = Regex.Matches(string1, pattern);


foreach (Match match in matches)
{
    Console.WriteLine(match.Groups[1].Value.ToString());
    Console.WriteLine(match.Groups[2].Value.ToString());
    Console.WriteLine();
}
 
Share this answer
 
Comments
Oguz Türkan 19-Dec-14 8:29am    
Hi I tried but too many ) s . sorry i beginner of regex

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