add some exception

Hi,

Could you please teach me how can I add some exception condition for the code.

<for example>
 if the input text  string "At the 27th week of  Year 1997,  2 cases [3]  were reported in 13 areas" . then  I would like exclude "3" to be counted as number. since it is the number inside "[ ]".

thank you
rmtogetherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Om PrakashCommented:
You can use replace function to remove [3] from the string.

const string s = "At the 27th week of  Year 1997,  2 cases [3]  were reported in 13 areas";
string v = s.Replace("[3]", "3");

Or use regular expressions do the same.
0
SriVaddadiCommented:
Could you elaborate on what exactly you are looking for? exception condition for code? May be a snippet would be more helpful
0
Gene_CypCommented:
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Roshan DavisCommented:
I am trying to combine the 2 regex (which I used in the below code). but still this works. is this ok for you?
int MAX_DIGIT = 4;
int []Count_array = {0,0,0,0};
string [] content_array = {string.Empty, string.Empty, string.Empty, string.Empty};
string strText = "At the 27th week of  Year 1997,  2 cases [3]  were reported in 13 areas";
Regex reInit = new Regex(@"(\[[0-9]\])+");
strText = reInit.Replace(strText, string.Empty);
Regex re = new Regex(@"[0-9]{1,4}");
MatchCollection mc = re.Matches(strText);

foreach(Match m in mc)
{
	Count_array[MAX_DIGIT - m.Length]++;
	if(content_array[MAX_DIGIT - m.Length] != string.Empty )
		content_array[MAX_DIGIT - m.Length] += ",";
	content_array[MAX_DIGIT - m.Length] += m.Value;
}

Open in new window

0
Roshan DavisCommented:
slight modification here
int MAX_DIGIT = 4;
int []Count_array = {0,0,0,0};
string [] content_array = {string.Empty, string.Empty, string.Empty, string.Empty};
string strText = "At the 27th week of  Year 1997,  2 cases [3]  were reported [312] in [455545] 13 areas";
Regex reInit = new Regex(@"(\[[0-9]+\])");
strText = reInit.Replace(strText, string.Empty);
Regex re = new Regex(@"[0-9]{1,4}");
MatchCollection mc = re.Matches(strText);

foreach(Match m in mc)
{
	Count_array[MAX_DIGIT - m.Length]++;
	if(content_array[MAX_DIGIT - m.Length] != string.Empty )
		content_array[MAX_DIGIT - m.Length] += ",";
	content_array[MAX_DIGIT - m.Length] += m.Value;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Pui_YunCommented:
Hi rmtogether,
What you are looking for is called a negative lookahead and negative lookbehind and what these do is peek in before/after each capture to see if it doesn't meet a condition, if it doesn't meet the condition then the value is captured.

However the value that we peek for is not captured (that's why these "peek" values are called zero-width assertions).  Most regular expression engines allow this and luckily the .NET regex engine does as well.

here are the zero width assertions I've used:
(?<!\[) - this checks to see that there is no left square bracket before the digits
(?!\]) - this checks to see that there is no right square bracket after the digits

BTW, here's a good tutorial that I use as a reference:
http://www.codeproject.com/KB/dotnet/regextutorial.aspx

I'm attaching the code with the modified regular expression below.

Hope this helps!
P.
int MAX_DIGIT = 4;
int []Count_array = {0,0,0,0};
string [] content_array = {string.Empty, string.Empty, string.Empty, string.Empty};
string strText = "At the 27th week of  Year 1997,  2 cases were reported in 13 areas";
Regex re = new Regex("(?<!\[)[0-9]{1,4}(?!\])");
MatchCollection mc = re.Matches(strText);

foreach(Match m in mc)
{
        Count_array[MAX_DIGIT - m.Length]++;
        if(content_array[MAX_DIGIT - m.Length] != string.Empty )
                content_array[MAX_DIGIT - m.Length] += ",";
        content_array[MAX_DIGIT - m.Length] += m.Value;
}

Open in new window

0
Pui_YunCommented:
Oops,
I've changed the text values so that you can test for it.

P.
int MAX_DIGIT = 4;
int []Count_array = {0,0,0,0};
string [] content_array = {string.Empty, string.Empty, string.Empty, string.Empty};
string strText = "At the 27th week of  Year 1997,  2 cases [3]  were reported [312] in [455545] 13 areas";
Regex re = new Regex("(?<!\[)[0-9]{1,4}(?!\])");
MatchCollection mc = re.Matches(strText);

foreach(Match m in mc)
{
        Count_array[MAX_DIGIT - m.Length]++;
        if(content_array[MAX_DIGIT - m.Length] != string.Empty )
                content_array[MAX_DIGIT - m.Length] += ",";
        content_array[MAX_DIGIT - m.Length] += m.Value;
}

Open in new window

0
rmtogetherAuthor Commented:
Thank you  roshmon, your code works for me.



Thank you Pui_Yun for instruction.
I find an unrecogonize error int the code you give me
Regex re = new Regex("(?<!\[)[0-9]{1,4}(?!\])");
0
Pui_YunCommented:
Sorry rmtogether,
I forgot to account escape the string and also account for the extra digits, here's the proper code:

P.
int MAX_DIGIT = 4;
int []Count_array = {0,0,0,0};
string [] content_array = {string.Empty, string.Empty, string.Empty, string.Empty};
string strText = @"At the 27th week of  Year 1997,  2 cases [3]  were reported [312] in [455545] 13 areas";
Regex re = new Regex(@"(?<!\[|\d)[0-9]{1,4}(?!\]|\d)");
MatchCollection mc = re.Matches(strText);

foreach(Match m in mc)
{
        Count_array[MAX_DIGIT - m.Length]++;
        if(content_array[MAX_DIGIT - m.Length] != string.Empty )
                content_array[MAX_DIGIT - m.Length] += ",";
        content_array[MAX_DIGIT - m.Length] += m.Value;
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.