Question: I have searched for the answer and have tried many different solutions but none seem to work!.

Using EveAI <http://wiki.eve-id.net/EveAI>

> 
> System.NullReferenceException: Object reference not set to an instance of an object. Form1.PopulateMarketIDs()
> 
> 
> 

```
private void PopulateMarketIDs()
{
    eapi.EveApi MyApi = new eapi.EveApi();
    List<int> MarketIDs = new List<int>();
    string SelectedItem;

    List<EveAI.Product.ProductType> Products = new List<EveAI.Product.ProductType>();

    foreach (string item in kryptonListBox1.Items)
    {
        SelectedItem = item;

        foreach (EveAI.Product.ProductType Prod in MyApi.EveApiCore.ProductTypes)
        {
            if (Prod.MarketGroup != null)
            {
                if (Prod.MarketGroup.ParentGroup.Name == SelectedItem)
                {
                    Products.Add(Prod);
                }
                else
                {
                    if (Prod.MarketGroup.Name == SelectedItem)
                    {
                        Products.Add(Prod);
                    }
                }
            }
        }
    }
}

```

The kryptonListBox1 contains at least one item such as "Mining Drones".
It works fine, it lists the products as it should, but when it gets to the last product (displays) and then crashes with the null error. 

I am aware that it will not return anything, I have snipped out the output section of it.

Answer: 
You can use this regex:

```
'/^[a-z0-9](?!.*?[._-]{2})[\w.-]*[a-z0-9]$/i'

```

This will not allow:

* Any special character at start & end
* Any special character one after another
How about an expression like this (not optimized but intentions should be clear)

```
'/^[a-z0-9]+[._-]?[a-z0-9]+$/i'

```

Only valid names will match this