I am very new to Regular expression so I apologise for the ‘noobyness’ of the question…
I need to match a pattern for an ID we use at work.
So far the only specification for the pattern is that it will be 9 characters long, and comprised of Capital letters and digits. The ID can contain 1 or any number of Capital letters or digits, so long as the total length of the string is 9 characters long.
So far i have the following… [A-Z][0-9]{9}
this does not makes sure the string has atleast one letter or digit (so a 9 character long string would pass)… Alos, Im sure it matched a 9 letter word made of non capitals.
I have done a fair bit of googling, but i have not found anything dumbed down enough for me to understand.
Any help very apppreciated 🙂
Thanks
EDIT: Just to recap the requirements – The id has to be 9 characters long, no more no less. It will be comprised of capital letters and digits. There can be any amount of either letter or digit, so long as the id contains atleast one of each (so BH98T6YUO or R3DBLUEEE or 1234R6789
I will also post my code to make sure that bits not wrong… ??
string myRegex = "A ton of different combinations that i have tried";
Regex re = new Regex(myRegex);
// stringCombos is a List<string> containing all my strings
// The strings contain within them, my id
// I am attempting to pull out this id
// the below is just to print out all found matches for each string in the list
foreach (string s in stringCombos)
{
MatchCollection mc = re.Matches(s);
Console.WriteLine("-------------------------");
Console.Write(s);
Console.WriteLine(" --- was split into the following:");
foreach (Match mt in mc)
{
Console.WriteLine(mt.ToString());
}
}
You actually have to learn regular expressions as a language. The curve is kind of steep, but there are a ton of excellent tutorials for the basics. Also, you might get this in a chat situation (SO has a chat functionality) – that is how I originally learnt them…
I think this might work for your case:
According to your update, for exactly 9 elements, use:
Note, though, that the requirement to include at least one letter and at least one digit is not expressed in this solution. An easy way to do that would be to apply a second and third match to the first one:
Thereby matching three times. You might be able to get this result with the fancy forward and backward reference stuff, but you cannot really capture that requirement with a regular grammar.