I have the following string:
AP00c8.8bf2.6b54
and I need a regexp to match it the following way:
starts with "AP"00c8(4chars).4chars.4chars
This is matching the whole string in 2 results:
^AP[a-zA-Z0-9]*|[a-zA-Z0-9]*\.[a-zA-Z0-9]+([a-zA-Z0-9]+)?$
So that is almost working, no I just need to limit the chars like 6.4.4
Anyone wanna lend me a hand?
-Toube
>Solution :
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.