Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to match a string (?,?,?) in Java

Tags:

java

regex

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 (?,?,?,?)

like image 395
Abhay Hegde Avatar asked Mar 03 '23 12:03

Abhay Hegde


1 Answers

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 = "\\(\\?(?:,\\?)+\\)";

Regex demo | Java demo

like image 132
The fourth bird Avatar answered Mar 13 '23 03:03

The fourth bird