Take the 2-minute tour ×

I am trying to work out a formula to match a following pattern: input string example:


if any of the patterns above exist in a particular input string I want to match it as the same pattern.

also is it possible to do a variable substitution using regex? meaning check for the 3 chars of the string by using each character as a variable and just doing an increment/decrement for each character? so that you dont have to specify the particular number ranges (hardcoding the pattern string ) for different patterns?

Is there any good library one can use for this?? I was working with Pattern class in java.

If you have any link which would be helpful please pass it through :) Thank you.

share|improve this question
And what about 343? –  Nikita Rybak Aug 23 '10 at 3:36
ya that also. (3/4)(3/4)(3/4) for each character. similarly wanted to do it for multiple numbers. so was looking for some kind of variable substitution ( for each character ) if that is possible in regex. –  JJunior Aug 23 '10 at 3:46
Personally, I have trouble understanding what exactly you want. Can you provide some examples? (unless codaddict answered your question, of course) –  Nikita Rybak Aug 23 '10 at 3:55

3 Answers 3

up vote 1 down vote accepted

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);

  // [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);

  // '[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'/'[aeiou]{5}'

This will now match strings like "'aeooi'/'eeiuu'/'uaooo'/'eeeia'/'eieio'".


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.

share|improve this answer
thanks so much! that was very detailed and helpful :) –  JJunior Aug 23 '10 at 13:13

You can use the regex:

share|improve this answer

Pattern.Compile takes a String as its parameter. Though that's probably most often supplied in the form of a string literal, if you have variable upper and lower bounds for your pattern, you can use something like StringBuilder to build your string, then pass that result to Pattern.Compile.

share|improve this answer

Your Answer


