0

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

2
  • Add the stacktrace please. Commented Aug 27, 2015 at 11:08
  • Tell us sample input and output. Commented Aug 27, 2015 at 11:10

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

this solves the error but i cant get the correct out put...can you help @anubhava ? What is wrong with this regular expression..?
yes .. 2 appears consecutively at least twice wg: 22 or 122 or 222 or 220
here c and freq are variables ... which are defined by user.. so c=2 and freq=2 as well..srry should have mentioned..
ya thnx its working...it clicked to to me as well.!!
Is there a way to check these regular expression on numbers without converting them into Strings...with String.matches() or is there any other function for that..?
|

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.