Regexp to match a string that starts with 2 letters

I have the following string:

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:


So that is almost working, no I just need to limit the chars like 6.4.4

Anyone wanna lend me a hand?


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


Here’s some more info about your original pattern, in case I misunderstood what you’re trying to do:

  1. | 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.
  2. * means "0 or more occurrences". You need {4} which means "4 occurrences".
  3. + means "1 or more occurrences". Again you need {4}.
  4. ? means "0 or 1 occurrences". Don’t need it.
  5. [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".
  6. () 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.

Leave a Reply