Reputation: 2695
I need regular expression which will start with 2 specific letters and will be 28 characters long. The regular expression is needed, as this is in conjunction with Spring configuration, which will only take a regular expression.
I've been trying to do with this, it's not working (^[AK][28]*)
Upvotes: 0
Views: 773
Reputation: 2220
If you need to count the number of repetitions use the {min, max}
syntax. Omiting both the comma and max tells the regex parser to look for exactly min
repetitions.
For example :
.{1,3}
will match for any character (shown by the dot
) sequence between 1 and 3 characters long.
[AK]{2}
will match for exactly 2 characters that are either A or K :
AK, AA, KA or KK.
Additionnaly, your regex uses [AK]. This means that it will match against one of the characters given, i.e. A or K.
If you need to match for the specific "AK" sequence then you need to get rid of the '[' ']' tokens.
Therefore you regex could be AK.{28}
meaning it will match for AK followed by exactly 28 characters.
Upvotes: 0
Reputation: 399
Regex is nothing specific to Java, nor is it that difficult if you have a look at any tutorial (and there's plenty!).
To answer your question:
AK[a-zA-Z]{26}
The above regex should solve your issue regarding a 28 character String with the first two letters being AK.
Elaboration:
In case you need it matching a whole line you would likely want to add ^ and $ at the beginning and end respectively, to tell the regex parser that it has to match a whole line/String and not just a part:
^AK[a-zA-Z]{26}$
Upvotes: 0
Reputation: 328598
If you mean that the string should be like "AKxxxxxxxx" (28 characters in total), then you can use:
^AK.{26}$ //using 26 since AK already count for 2 characters
Upvotes: 3