Let's first consider this pattern: [34]{3}
The […]
is a character class, it matches exactly one of the characters in the set. The {n}
is an exact finite repetition.
So, [34]{3}
informally means "exactly 3 of either '3'
or '4'
". Thus, it matches "333"
, "334"
, "343"
, "344"
, "433"
, "434"
, "443"
, "444"
, and nothing else.
As a string literal, the pattern is "[34]{3}"
. If you don't want to hardcode this pattern, then just generate similar-looking strings that follows this template "[…]{n}"
. Just put the characters that you want to match in the …
, and substitute n
with the number you want.
Here's an example:
String alpha = "aeiou";
int n = 5;
String pattern = String.format("[%s]{%s}", alpha, n);
System.out.println(pattern);
// [aeiou]{5}
We've now seen that the pattern is not hardcoded, but rather programmatically generated depending on the values of the variables alpha
and n
. The pattern [aeiou]{5}
will 5 consecutive lowercase vowels, e.g. "ooiae"
, "ioauu"
, "eeeee"
, etc.
It's again not clear if you just want to match these kinds of strings, or if they have to appear like '…'/'…'/'…'/'…'/'…'
. If the latter is desired, then simply compose the pattern as desired, using repetition and grouping as necessary. You can also just programmatically copy and paste the pattern 5 times if that's simpler. Here's an example:
String p5 = String.format("'%s'/'%<s'/'%<s'/'%<s'/'%<s'", pattern);
System.out.println(p5);
// '[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'
This will now match strings like "'aeooi'/'eeiuu'/'uaooo'/'eeeia'/'eieio'"
.
Caveat
Do be careful about what goes in alpha
. Specifically, -
, [
. ]
, &&
, ^
, etc, are special metacharacters in Java character class definition. If you restrict alpha
to contain only digits/letters, then you will probably not run into any problems, but e.g. [^a]
does NOT mean "either '^'
or 'a'
". It in fact means "anything but 'a'
. See java.util.regex.Pattern
for exact character class syntax.