Skip to main content
  1. About
  2. For Teams
Asked
Viewed 2k times
1

WPF treats a single underscore as a mnemonic in the content of a Button

However, it is likely that the content will need to contain an underscore.

The content is defined by the user, and there is nothing to stop them having multiple underscores, sequentially or not. EG

This_Is It for the__moment and this is three___of the things

If I assign the above nonsense string to a Button.Content, it will treat the single underscore as a mnemonic and will result in ThisIs It for the__moment and this is three___of the things (note the _ is missing and I now have ThisIs as one word). What I want it to update it This__Is It for the__moment and this is three___of the things (note it's now double underscore, but the other occurrences of the underscore remain unchanged).

This is what I have, it's just so clunky (although it works).

    static void Main(string[] args)
    {
        Console.WriteLine(Other("This_Is It for the__moment and this is three___of the things"));
        Console.ReadKey(); // result is This__Is It for the__moment and this is three___of the things  (note the double __ after the first word This)
    }

    static string Other(string content)
    {
        List<int> insertPoints = new List<int>();
        for (int i = 0; i < content.Length; i++)
        {
            char current = content[i];
            if (content[i] == '_' && content[i + 1] != '_')
            {
                if (i - 1 >= 0)
                    if (content[i - 1] == '_')
                        continue;

                insertPoints.Add(i);
            }
        }

        foreach (var item in insertPoints)
        {
          content =  content.Insert(item, "_");
        }

        return content;
     }

My question is, would there be less code with a RegEx?

4
  • what output are you expecting ?
    aelor
    –  aelor
    2014-03-20 12:34:43 +00:00
    Commented Mar 20, 2014 at 12:34
  • 1
    Why would a simple string.Replace not work here? It is because you are using ReadKey?
    Charleh
    –  Charleh
    2014-03-20 12:41:59 +00:00
    Commented Mar 20, 2014 at 12:41
  • @Charleh, because it would replace every time it found '_', thus updating the original string value on screen from the original!?
    Dave
    –  Dave
    2014-03-20 12:42:44 +00:00
    Commented Mar 20, 2014 at 12:42
  • @DaveRook - please, if you get a moment, consider my answer - I think it is definitely possible to use String.Replace() - and seems to be more efficient too... (?)
    Code Jockey
    –  Code Jockey
    2014-03-20 13:43:01 +00:00
    Commented Mar 20, 2014 at 13:43

3 Answers 3

4

You could use the following regex to find the single underscores:

(?<!_)_(?!_)

Then replace it with two underscores.

(?<!_) is a negative lookbehind; it prevents the matched underscore to be preceded by another underscore.

(?!_) is a negative lookahead; it prevents the matched underscore to be followed by another underscore.

regex101 demo


You'll need to use using System.Text.RegularExpressions; and you can use it like below:

var regex = new Regex(@"(?<!_)_(?!_)");
var result = regex.Replace(str, "__");
Sign up to request clarification or add additional context in comments.

4 Comments

awwww i posted a same answer :)
+1 for describing things (like lookbehind), though in this particular case, the @ isn't really needed... I wonder what the difference is in compilation when you don't need it (?) hmmmmm.....
+1 for the things I forgot (Verbatim string (@) and the result variable).
@CodeJockey Thanks! Hmm, I'm not sure myself, but I made it a rule to make regex strings verbatim (better be safe than sorry!). That also avoids having to double escape if someone edits the regex and introduces something that needs to be double escaped otherwise :)
2

The following c# code should do what you need.

var reg = new Regex("(?<!_)_(?!_)");
reg.Replace("your string","__");

Comments

1

you can do a lookaround check

(?<!_)\_(?!_)

and replace the underscore with double underscore

demo here : http://regex101.com/r/bD1cI9

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.