|
I have a string like:
string s1 = "dog().Cat(\"Happy\")";
I want to:
1) count the number of round brackets '(', I expect as result 2
2) get the text inside, I expect "\"Happy"\", which appear in console output as "Happy"
No idea for 1. I try the following for 2
string k = Regex.Match(s1,@"\((\w+)\)").Groups[1].Value;
But It fails in understanding the \" character
Any Idea?
|
|
|
|
|
You're nearly there:
string k = Regex.Match(s1, @"\(""(\w+)""\)").Groups[1].Value;
For #1, unless you need to ensure that the brackets are balanced, Regex is overkill. Add using System.Linq; to your file, and try:
int count = s1.Count(c => c == '(');
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Thanks you get the point.
|
|
|
|