stuartdotnet
stuartdotnet

Reputation: 3034

Regex for Azure blob containers

Trying to create a regex to use in the following code to ensure my input conforms to Azure blob container rules, but needing a regex means I have 2 problems.

Rules:

Not worried about lower case as I am going to .ToLower() it after this.

Tried this but it keeps $ and ^ so I must be doing something wrong?

Regex rgx = new Regex(@"^[a-zA-Z][a-zA-Z0-9]*$");

Upvotes: 1

Views: 2754

Answers (2)

Shiroy
Shiroy

Reputation: 1818

The latest version of the Azure Storage Library now contains this method:

   Microsoft.WindowsAzure.Storage.NameValidator.ValidateContainerName(myContainerName);

If the name is not valid it will throw an ArgumentException.

Better than trying to make your own.

Upvotes: 2

Gaurav Mantri
Gaurav Mantri

Reputation: 136206

Try this instead:

        Regex regEx = new Regex("^[a-z0-9](?:[a-z0-9]|(\\-(?!\\-))){1,61}[a-z0-9]$|^\\$root$");
        var isContainerNameValid = regEx.IsMatch(containerName);

Source: Azure Portal --> New Container creation screen --> View Source :)

Upvotes: 8

Related Questions