Subhash
Subhash

Reputation: 383

PatternSyntaxException while using string,match()

I'm getting a pattern syntax exception in this regular expression:

[^c]*[c]{freq}[^c]*

It checks for the multiple occurrence of the letter C (equal to frequency or amount of times).

Upvotes: 0

Views: 62

Answers (1)

anubhava
anubhava

Reputation: 785246

You cannot use freq variable in regex like this. Build your regex as a String:

String regex = "[^c]*c{" + freq + "}[^c]*";

If c is also a variable then use:

String regex = "[^" + c + "]*" + c + "{" + freq + "}[^" + c + "]*";

RegEx Demo

Upvotes: 1

Related Questions