I have an input containing repeated pattern like this:
(3,5)(6,7)(8,9).....
I am working on a regex to validate the above string pattern.
I tried:
Pattern.compile("\\((\\d+),(\\d+)\\)")
I have an input containing repeated pattern like this:
(3,5)(6,7)(8,9).....
I am working on a regex to validate the above string pattern.
I tried:
Pattern.compile("\\((\\d+),(\\d+)\\)")
If you have a specific pattern that may repeat one or more times in a string, and you want to make sure your string only consists of the repeated occurrences of the same pattern, you may use
^(?:YOUR_PATTERN)+$
\A(?:YOUR_PATTERN)+\z
Note that $ also matches before a final newline char, that is why \z anchor is preferred when you need to match the very end of the string.
If you allow an empty string use the * quantifier instead of +:
^(?:YOUR_PATTERN)*$
\A(?:YOUR_PATTERN)*\z
In this case, the YOUR_PATTERN is \(\d+,\d+\), thus, the repeated sequence validating pattern will be \A(?:\(\d+,\d+\))+\z.
In Java, you may omit the ^/$ and \A/\z anchors when validating a string with .matches() method since it requires a full string match:
boolean isValid = text.matches("(?:\\(\\d+,\\d+\\))+");
Or, decalre the Pattern class instance first and then create a matcher with the string as input and run Matcher#matches()
Pattern p = Pattern.compile("(?:\\(\\d+,\\d+\\))+");
Matcher m = p.matcher(text);
if (m.matches()) {
// The text is valid!
}