Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text.RegularExpressions;
- public class SumOfAllValues
- {
- public static void Main()
- {
- const string keysPattern = @"^(?<start>[a-zA-Z_]+)[0-9].*?[0-9](?<end>[a-zA-Z_]+)$";
- string keysString = Console.ReadLine();
- var match = Regex.Match(keysString, keysPattern);
- string start = match.Groups["start"].Value;
- string end = match.Groups["end"].Value;
- if (start == string.Empty || end == string.Empty)
- {
- Console.WriteLine("<p>A key is missing</p>");
- return;
- }
- string text = Console.ReadLine();
- string numberPattern = string.Format("{0}(.*?){1}", start, end);
- var numbers = Regex.Matches(text, numberPattern);
- double sum = 0;
- foreach (Match numberMatch in numbers)
- {
- double number;
- double.TryParse(numberMatch.Groups[1].Value, out number);
- sum += number;
- }
- Console.WriteLine(
- "<p>The total value is: <em>{0}</em></p>",
- sum == 0 ? "nothing" : sum.ToString());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement