Regex 101 Exercise I9 - Count the number of matches
2006-03-01 21:22
423 查看
After a long break, Eric is back, and continue his awesome Regex 101 Exercise series, in this exercise, the question Eric asks is:
-------------------------------------------------------------------------------------------------
Given a string like:
# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23
Count how many numbers there are in this string
--------------------------------------------------------------------------------------------------
I have three simple solutions to this problem.
Solution #1:
Regex regex = new Regex(@"\d+", RegexOptions.IgnoreCase);
String inputString = @"# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23";
Int32 count = 0;
regex.Replace(inputString, delegate(Match match)
{
count++;
return String.Empty;
});
Console.WriteLine(count);
Solution #2:
Regex regex = new Regex(@"\d+", RegexOptions.IgnoreCase);
String inputString = @"# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23";
MatchCollection matches = regex.Matches(inputString);
Console.WriteLine(matches.Count);
Solution #3:
Regex regex = new Regex(@"[#\s]+", RegexOptions.IgnoreCase);
String inputString = @"# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23";
String[] values = regex.Split(inputString);
Console.WriteLine(values.Length - 1);
Side note: For all of you who are interested in regluar expressions, and want to be more proficient at it, I encourage you to actively participate in Eric Gunnerson's regex exercises, at the end of day, you will find that you benefit a lot in that process.
-------------------------------------------------------------------------------------------------
Given a string like:
# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23
Count how many numbers there are in this string
--------------------------------------------------------------------------------------------------
I have three simple solutions to this problem.
Solution #1:
Regex regex = new Regex(@"\d+", RegexOptions.IgnoreCase);
String inputString = @"# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23";
Int32 count = 0;
regex.Replace(inputString, delegate(Match match)
{
count++;
return String.Empty;
});
Console.WriteLine(count);
Solution #2:
Regex regex = new Regex(@"\d+", RegexOptions.IgnoreCase);
String inputString = @"# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23";
MatchCollection matches = regex.Matches(inputString);
Console.WriteLine(matches.Count);
Solution #3:
Regex regex = new Regex(@"[#\s]+", RegexOptions.IgnoreCase);
String inputString = @"# # 4 6 # # 7 # 45 # 43 # 65 56 2 # 4345 # # 23";
String[] values = regex.Split(inputString);
Console.WriteLine(values.Length - 1);
Side note: For all of you who are interested in regluar expressions, and want to be more proficient at it, I encourage you to actively participate in Eric Gunnerson's regex exercises, at the end of day, you will find that you benefit a lot in that process.
相关文章推荐
- count the number of 2s
- Count the number of 1(MIT hackmem)
- python int to binary and count the number of 1
- quick count the number of the rows in a big table
- How to count the occurrences of a number?
- *Count the number of possible triangles
- How to count the number of threads in a process on Linux
- COJ 1068: Count the Number of Cycles(向量点积)
- OpenCV count the number of connected camera 检测连接的摄像头的数量
- Count the number of occurrences in a sorted array
- Error Number : 1547 Error Message: Column count of mysql.proc is wrong. Expected 20, found 16. The
- leetcode:count the number of nodes
- 非零比特的个数 count the number of bits
- Q9.11 count the number of ways of parenthesizing the expression
- [geeksforgeeks] Count the number of occurrences in a sorted array
- function to count the number of "1" of one byte
- count the number of 1 in a decimal integer
- FB面经prepare: Count the number of Vector
- count the number of "1" in the binary figure
- Write a method to count the number of 2s between 0 and n