Question: 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?

Answer: 
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](http://regex101.com/r/sK5lV0)

---

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, "__");

```
you can do a lookaround check

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

```

and replace the underscore with double underscore

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