regex – Regexp to match a string that starts with 2 letters
If I understand correctly, you need to match strings that start with “AP” followed by 4 alphanumerics, followed by a dot, followed by 4 alphanumerics, followed by a dot, followed by another 4 alphanumerics. That’s:
^AP[a-zA-Z0-9]{4}\.[a-zA-Z0-9]{4}\.[a-zA-Z0-9]{4}$
Here’s some more info about your original pattern, in case I misunderstood what you’re trying to do:
^AP[a-zA-Z0-9]*|[a-zA-Z0-9]*\.[a-zA-Z0-9]+([a-zA-Z0-9]+)?$
|means “or”. You don’t need that. It defines two alternative patterns, one that starts with “AP” etc, and one that ends with an optional group of alphanumerics.*means “0 or more occurrences”. You need{4}which means “4 occurrences”.+means “1 or more occurrences”. Again you need{4}.?means “0 or 1 occurrences”. Don’t need it.[a-zA-Z0-9]+([a-zA-Z0-9]+)?means “one or more alphanumerics optionally followed by another group of one or more alphanumerics”. This doesn’t make much sense. When should the first group stop and the second group begin? Just[a-zA-Z0-9]+matches the same thing: “one or more alphanumerics”.()the parentheses are a capturing group. They mean “capture this part of the match into a variable because I want to use it later in my code”.
I hope this helps.
Read more here: Source link
