I am using:

underscoredWord = Regex.Replace(word, "[^" + correctLetters + "]", " _");

for my Hangman game. It basically chops the word up and replaces all the text with an underscore.

This works fine if the guessing word is only "one word". So the word: "Building" will get converted to: _ _ _ _ _ _ _ _

How do I improve the above Regex express so that it puts underscores for all letters and a forward slash "/" for spaces?

So the words: Hello Batman

Be turned in to: _ _ _ _ / _ _ _ _ _

Any and all ideas/contributions to this post will be highly appreciated.

I believe (and someone please correct me if I'm wrong) that you will need to do a separate replace for the space->slash.
Although you could match both cases using and OR case in the Regex, I don't think there is any way to specify different replacement text for each case in a single statement.

Something like this should work: underscoredWord = Regex.Replace(word.Replace(" ","/"), "[^" + correctLetters + "]", " _");

For some reason that didn't work.

The code:

Regex.Replace(word.Replace(" ","/"), "[^" + correctLetters + "]", " _");

Is done when someone puts clicks on a letter. I think I need to change the code which first makes the underscores.

This is the code:

for (int i = 0; i < word.Length; i++)
                {
                    label += "_ "; //iterates until the amount of characters within the word is met
                }

The above code simply makes a string made up of underscores. I need to modify this I think. Any idea how I can put a forward slash in here?

How about something like this?:

using System;
using System.Text.RegularExpressions;

namespace DW_419823_CS_CON
{
   class Program
   {
      private static string MakeDashes(string strPhrase)
      {
         return Regex.Replace(strPhrase.Replace(' ', '/'), @"[a-z,A-Z]", "_");
      }

      static void Main(string[] args)
      {
         string strPhrase = "this is neat";
         Console.WriteLine(MakeDashes(strPhrase));
      }
   }
}

Sorry, I misunderstood something:

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace DW_419823_CS_CON
{
   class Program
   {
      private static string MakeDashes(string strPhrase, string strCorrectLetters)
      {
         string strAlphaPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         string strReplace = '['+
            string.Join(",", 
               strAlphaPattern.Where(s => !strCorrectLetters.ToUpper()
                  .Contains(s)).Select(s => s.ToString()).ToArray())
                  +']';

         return Regex.Replace(strPhrase.ToUpper().Replace(' ', '/'), strReplace, "_");
      }

      static void Main(string[] args)
      {
         string strPhrase = "this is neat";
         Console.WriteLine(MakeDashes(strPhrase, "rstlne"));
      }
   }
}

Here is an updated, cosmetic version:

using System;
using System.Linq;
using System.Text.RegularExpressions;

namespace DW_419823_CS_CON
{
   class Program
   {
      private static string MakeDashes(string strPhrase, string strCorrectLetters)
      {
         string strAlphaPattern = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

         // Remove all correct letters from the alphabet and re-form it as a pattern
         string strReplacePatt = '['+
            string.Join(",", 
               strAlphaPattern.Where(s => !strCorrectLetters.ToUpper()
                  .Contains(s)).Select(s => s.ToString()).ToArray())
                  +']';

         return
            string.Join(" ", // put in spaces for cosmetic reasons AFTER conversion
            Regex.Replace(strPhrase.ToUpper().Replace(' ', '/'), strReplacePatt, "_")
            .Select(s => s.ToString()).ToArray());
      }

      static void Main(string[] args)
      {
         string strPhrase = "this is neat";
         Console.WriteLine(MakeDashes(strPhrase, "rstlne"));
      }
   }
}
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.