Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
1.75/5 (4 votes)
See more:
Hi all,


I can extract a string of text that lies between two (brackets)
C#
string input = "soap(999)";
String SID =     input.Remove(input.IndexOf(')')).Substring(input.IndexOf('(') + 1);

output: 999

This is fine, But my need is

C#
String input = "soap(small)(999)";

If my string is like this means,I am not able to retriev id from string "999"


Note: In my String always "id" will be in last position only

Thanks in Advance.
Posted
Updated 7-Dec-11 9:08am
v2
Comments
Sergey Alexandrovich Kryukov 7-Dec-11 10:41am    
How it is related to "windows application" or any kind of application? (It is not.)
--SA
[no name] 7-Dec-11 20:29pm    
Not quite really clear. Please give us a good INPUT and OUTPUT that you want

It is not clear what do you mean by "between two brackets". You might not think about it, but "(small)(999)" is another kind of match, but you probably need "(small)" or "(999)", which is called a lazy pattern.

You can find all kinds of matches using Regular Expressions. For example:

C#
using System.Text.RegularExpressions;

//...

string sampleInput = "soap(small)(999)";
Regex regex = new Regex(@"\(.*?\)");
MatchCollection matches = regex.Matches(sampleInput);
int count = matches.Count;

//...

string s0 = matches[0].Result; //will give you (small) 
string s1 = matches[1].Result; //will give you (999) 


But if you use

C#
Regex regex = new Regex(@"\(.*\)");
you will get only one match "(small)(999)".

See:
http://msdn.microsoft.com/en-us/library/hs600312.aspx[^],
http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx[^],
http://en.wikipedia.org/wiki/Regular_expression[^].

—SA
 
Share this answer
 
Comments
Michel [mjbohn] 7-Dec-11 15:55pm    
Match.Result() is a function for match replacement. Match.Value contains the result of the match. As far as I remember.
vempadu 8-Dec-11 0:06am    
Thanks For reply,
It is working fine.But my need is to check a string from "End of
String only" Because in my string "id" is in last position only
Ex:String x = "sanju(sss)(www)(9999)(2222)";
In this string i required last two(brackets) substring.
x = "2222"
How 'bout:
C#
using System.Text.RegularExpressions;

		
string myString = "soap(small)(999)";
string insideParentheses = Regex.Match(myString, "([0-9][0-9][0-9])").Groups(0).ToString();
 
Share this answer
 
Comments
lewax00 7-Dec-11 17:58pm    
"([0-9]{3})" or "([0-9]+)" (if it's a variable number or digits) might be better options. But otherwise that looks good.
Scubapro 8-Dec-11 1:48am    
Your absolutely right, I forgot about that.
Member 14154128 2-Apr-19 0:28am    
if there is multiple parentheses how will you deal with it, as I want to capture all the datas hide in parentheses like (a+b(c*d))
string xx = y.Substring(y.LastIndexOf("(") + 1, (y.LastIndexOf(")") - y.LastIndexOf("(")) - 1);
 
Share this answer
 
Thanks ... Second Solution helped me :)
 
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