0

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.

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by thines01
0

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 + "]", " _");

0

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?

Edited by Nick Evan: Fixed formatting

0

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));
      }
   }
}
0

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"));
      }
   }
}

Edited by thines01: typo

0

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"));
      }
   }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.