Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regexp to match a string that starts with 2 letters

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

^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]+)?$
  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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading