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?
 
Roshan DavisConnect With a Mentor Commented:
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Gene_CypCommented:
0
 
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
 
Pui_YunConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.