Hi all, I'm learning how to format strings. I've made the following code, but the console screen just comes up blank.

static void Main(string[] args)
        {
            DateTime ci = DateTime.Now;     //instance of datetime
            Console.WriteLine("{0:D}", ci);

            string info = "Fred Savage- SSN: 143-14-8756 Phone: (281)414-3424 Email: fsavage@blah.com";
            string format = @"(?<name>Fred\d+\s+)\s*(?<ss>SSN:\d+\s+)\s*(?<phone>Phone:\d+\s+)\s*(?<email>Email:\d+\s+)";
            Match nameMatch = Regex.Match(info, format);
            if (nameMatch.Success)
            {
                Console.Write(nameMatch.Groups[1]);
                Console.Write(nameMatch.Groups[2]);
                Console.Write(nameMatch.Groups[3]);
                Console.Write(nameMatch.Groups[4]);
            }
            
            Console.ReadKey();



        }

Any help would be appreciated

By comes up blank do you mean that you do not even see the DateTime output in the console window? I ran this as a console application and I see the date time print but not the regular expression since the info and pattern do not match. If that is what you mean by blank then you can change your regex around a little bit. This is a very strict expression but using your example:

New expression:

const string info = "Fred Savage- SSN: 143-14-8756 Phone: (281)414-3424 Email: fsavage@blah.com";
      const string format = @"(?<name>Fred.*)-\s.*(?<ss>SSN:\s\d{3}-\d{2}-\d{4}).*(?<phone>Phone:\s.*\d\s).*(?<email>Email:.*@.*..*)";

Or the method entirely:

static void Main(string[] args)
    {
      DateTime ci = DateTime.Now;     //instance of datetime
      Console.WriteLine("{0:D}", ci);

      const string info = "Fred Savage- SSN: 143-14-8756 Phone: (281)414-3424 Email: fsavage@blah.com";
      const string format = @"(?<name>Fred.*)-\s.*(?<ss>SSN:\s\d{3}-\d{2}-\d{4}).*(?<phone>Phone:\s.*\d\s).*(?<email>Email:.*@.*..*)";
      Match nameMatch = Regex.Match(info, format);
      if (nameMatch.Success)
      {
        Console.WriteLine(nameMatch.Groups[1]);
        Console.WriteLine(nameMatch.Groups[2]);
        Console.WriteLine(nameMatch.Groups[3]);
        Console.WriteLine(nameMatch.Groups[4]);
      }

      Console.ReadKey();
    }

Once again, thanks Scott, that works perfectly. :D

Out of curiosity was the real problem the Regular expression not matching? I thought it was very odd if the date wasn't displaying on the console :)

Yes the date was displaying fine. It was the RegEx that I couldn't get to be displayed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.