My input string is of the form: String input = "(?,?,?)";
I am not able to come up with a valid regex to identify such strings
I have tried with following regex:
String regex = "(\\?,*)";
Assertion fails with the above regex for input strings such as (?,?) or (?,?,?,?)
You could match (?
and then repeat 1+ times ,?
and match )
.
If a single question mark is also valid, you could change the quantifier from +
to *
\(\?(?:,\?)+\)
Explanation
\\(\\?
Match (?
(?:,\\?)+
Non capturing group, repeat 1+ times ,?
\\)
Match )
In Java
final String regex = "\\\\(\\\\?(?:,\\\\?)+\\\\)";
A general regex pattern would be:
\(\?(?:,\?)*\)
Sample script:
String input = "(?,?,?,?)";
if (input.matches("\\(\\?(?:,\\?)*\\)")) {
System.out.println("MATCH");
}
Here is an explanation of the regex:
\( match a literal opening parenthesis
\? match a single ?
(?:,\?)* then match ,? zero or more times
\) then match closing )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.