0

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]*)

7
  • 2
    specify length using {} not [] Commented Jun 4, 2015 at 7:38
  • And also [AK] matches either A or K, not two characters, Commented Jun 4, 2015 at 7:38
  • You can also very easily do this without a regular expression: s.startsWith("AK") && s.length == 28. Don't overuse regular expressions, they are not always the best solution for every possible problem. Commented Jun 4, 2015 at 7:43
  • I need regex because this is in spring configuration. I accepted @assylias answer, Commented Jun 4, 2015 at 7:51
  • Then you should add the Spring tag, as spring doesn't always match regex like you would think Commented Jun 4, 2015 at 8:03

3 Answers 3

3

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
Sign up to request clarification or add additional context in comments.

4 Comments

if I want length of the string to be undefined, what should be in the brackets? Is it a * ? thx
yes replace .{26} with .* for any string (including empty) or .+ for a non empty string.
.{26} doesn't match 26 characters but 26 symbols, which can be anything from a-zA-Z over \d to .,-() etc.
@dot_Sp0T A character can be anything: character != letter... This is the official terminology, see for example the javadaoc for Pattern...
0

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:

  • AK[a-zA-Z]{26}
    > Characters written as such, without any special characters will be matched as is (that means they must be where they were written, in exactly that fashion)
  • AK[a-zA-Z]{26}
    > By using square brackets you can define a set of characters, signs, etc. to be matched against a part of the regex (1 by default) - you can write down all the possible characters/signs or make use of groups (e.g. a-z, /d for digits, and so forth)
  • AK[a-zA-Z]{26}
    > for each set of characters/signs you can define a repetition count, this defines how often the set can/must be applied. E.g. {26} means it must match 26 times. Other possibilities are {2, 26} meaning it must match at least 2 times but at most 26 times, or for example use an operator like *, + or ? which denote that the set can be matched 0 or more times, at least once or 0 or 1 time

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}$

2 Comments

[a-zA-Z] only matches letters but the OP asked for a regex to match any characters (unless he did mean letter)...
@assylias regarding the context given in their comments I assume him to mean characters as in letters. As special characters, e.g. /, do lead to other results in parts of Springs regex parsing
0

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 minrepetitions.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.