rmtogether
asked on
parse (find) number in the text string
How can I find numbers in text string by C#. for example
<Example>
A text string "At the 27th week of Year 1997, 2 cases were reported in 13 areas"
Can I have the return in dynamic array (or array list), which can automatically adjust the size of array.
4 digit is the max. I won't have anything above 4 digit.
so in this example, it will return.
content_array (return those numbers)
1997 --> number with 4 digit
Null --> number with 3 digit
27, 13 --> number with 2 digit
2 --> number with 1 digit
Count_array (return count of numbers)
1--> number with 4 digit
0--> number with 3 digit
2 --> number with 2 digit
1 --> number with 1 digit
Thanks in advance
<Example>
A text string "At the 27th week of Year 1997, 2 cases were reported in 13 areas"
Can I have the return in dynamic array (or array list), which can automatically adjust the size of array.
4 digit is the max. I won't have anything above 4 digit.
so in this example, it will return.
content_array (return those numbers)
1997 --> number with 4 digit
Null --> number with 3 digit
27, 13 --> number with 2 digit
2 --> number with 1 digit
Count_array (return count of numbers)
1--> number with 4 digit
0--> number with 3 digit
2 --> number with 2 digit
1 --> number with 1 digit
Thanks in advance
easiest way is regex, the code would be something like this
string strText = "At the 27th week of Year 1997, 2 cases were reported in 13 areas";
Regex re = new Regex("[0-9]{1,4}");
//string resultStr = re.Replace(strText, " ");
MatchCollection mc = re.Matches(strText);
foreach(Match m in mc)
{
MessageBox.Show(m.Length.ToString() + ", " + m.Value);
}
if you don't have MAX 4 digit restriction, you can do this
string strText = "At the 27th week of Year 1997, 2 cases were reported in 13 areas";
Regex re = new Regex("[0-9]+");
//string resultStr = re.Replace(strText, " ");
MatchCollection mc = re.Matches(strText);
foreach(Match m in mc)
{
MessageBox.Show(m.Length.ToString() + ", " + m.Value);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi, roshmon
I would like have little extension about this code. Could you please help me about this.
Please let me know you are around, and I will post a new topic. thank you
I would like have little extension about this code. Could you please help me about this.
Please let me know you are around, and I will post a new topic. thank you
sure, 10:24 PM now, I can look into that in the morning :)
ASKER
thank you roshmon
I guess we are in same time zone.
this is my question link. please help me about this. thanks in advance.
https://www.experts-exchange.com/questions/25760037/add-some-exception.html
I guess we are in same time zone.
this is my question link. please help me about this. thanks in advance.
https://www.experts-exchange.com/questions/25760037/add-some-exception.html
ASKER
4 digit is the max. I won't have anything above 4 digit.