Home > Software engineering >  Regex for 4-Block-Pattern combined with min and max lentgh
Regex for 4-Block-Pattern combined with min and max lentgh

Time:01-21

I a have a formular that is in 4-Block format (e.g. 1232 2131 3233 2323 23).

What I want is to check an input String, if it machtes this pattern BUT with a maximum length.

I have found a way for a fixed length....e.g. ".{4} .{4} .{2}" for fixed length 10 (the whitespaces should not count to length)

But how has the regex to look like if I want to check the 4-Block-pattern for a variable length with a max count of charactrs.

Let´s say 4-Block for max-length of 10 characters which would allow all of these: "XXX", "XXXX", "XXXX XX", "XXXX XXXX", "XXXX XXXX " and "XXXX XXXX XX".

I try to build a java function with that signature:

public static String getRegex(int blockSize, int maxLength) {

}

Thank you for your mindpower.

CodePudding user response:

You can group signs using parenthesis. And giving two numbers in {} with comma between means min and max occurs. For situation you mentioned: (.{4} .{4}){1,2}. So it would check whether it is: XXXX XXXX or XXXX XXXX XXXX XXXX If you want check if only this pattern is in string you should also add '^' at the beginning and '$' at the end. Then it should look like: ^(.{4} .{4}){1,2}$

^ means beginning of a line $ means end of a line

CodePudding user response:

I would first calculate the max length including possible whitespaces, which depends on the number and the size of the blocks. So say for a block size of 4:

  • if max length is 7, max number of whitespaces is 1 (XXXX XXX)
  • if max length is 8, max number of whitespaces is 1 (XXXX XXXX)
  • if max length is 9, max number of whitespaces is 2 (XXXX XXXX X)

This can be generalised as follow:

int numberOfWhitespacesAllowed = (maxLength - 1) / blockSize;
int maxLengthIncludingWhitespaces = maxLength   numberOfWhitespacesAllowed;

Now we know this, we can more easily build the regex, by first checking the general pattern:

^(\S{<blocksize>}\s)*\S{1,<blocksize>}$

For example for a block size of 4:

^(\S{4}\s)*\S{1,4}$

And then adding the check for the max length by using a positive lookahead:

^(?=.{1,<maxLengthIncludingWhitespaces>}$)

For example for a max length (including whitespace) of 9:

^(?=.{1,9}$)(\S{4}\s)*\S{1,4}$

All together in Java:

public static String getRegex(int blockSize, int maxLength) {
    int numberOfWhitespacesAllowed = (maxLength - 1) / blockSize;
    int maxLengthIncludingWhitespaces = maxLength   numberOfWhitespacesAllowed;
    return "^(?=.{1,"   maxLengthIncludingWhitespaces   "}$)(\\S{"   blockSize   "}\\s)*\\S{1,"   blockSize   "}$";
}

Which gives for block size of 4:

  • for max length of 7: ^(?=.{1,8}$)(\S{4}\s)*\S{1,4}$
  • for max length of 8: ^(?=.{1,9}$)(\S{4}\s)*\S{1,4}$
  • for max length of 9: ^(?=.{1,11}$)(\S{4}\s)*\S{1,4}$
  •  Tags:  
  • Related