Gowthamss
Gowthamss

Reputation: 241

Regex to match only the following strings

I have a few strings and I need some help with constructing Regex to match them.

The example strings are:

AAPL10.XX1.XX2
AAA34CL
AAXL23.XLF2
AAPL

I have tried few expressions but couldn't achieve exact results. They are of the following:

[0-9A-Z]+\.?[0-9A-Z]$
[A-Z0-9]*\.?[^.]$

Following are some of the points which should be maintained:

  1. The pattern should only contain capital letters and digits and no small letters are allowed.
  2. The '.' in the middle of the text is optional. And the maximum number of times it can appear is only 2.
  3. It should not have any special characters at the end.

Please ask me for any clarification.

Upvotes: 0

Views: 37

Answers (1)

The fourth bird
The fourth bird

Reputation: 163362

You can write the pattern as:

^[A-Z\d]+(?:\.[A-Z\d]+){0,2}$

The pattern matches:

  • ^ Start of string
  • [A-Z\d]+ Match 1+ chars A-Z or a digit
  • (?:\.[A-Z\d]+){0,2} Repeat 0 - 2 times a . and 1+ chars A-Z or a digit
  • $ End of string

Regex demo

Upvotes: 2

Related Questions