Reputation: 3034
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
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
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