简体   繁体   中英

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:

  • Letters and numbers and hyphens only
  • No spaces
  • Must start with a letter or number

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]*$");

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 :)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM