I am trying to create regexp to find duplicated commas, like here:
baz(uint32,,bool)
For now my pattern is : \w*\([\w\[\],?+]+\)
which is not working.
How can one specify quantity for items in character class?
You cannot specify a number of occurrences inside a character class since this construct is used to define a specific character type. Inside [...]
, the *
, +
, ?
, {1,2}
are treated as literal symbols.
If you need to just match several comma separated words inside parentheses, use
\w*\(\w*(?:,\w*)*\)
or with obligatory first word:
\w+\(\w*(?:,\w*)*\)
^
See the regex demo (or this one).
In Java, use String re = "\\w+\\(\\w*(?:,\\w*)*\\)";
.
Pattern details:
\w*
- 0+ word chars\(
- one literal (
\w*
- 0+ word chars(?:,\w*)*
- zero or more sequences (the (?:...)
and (...)
define sequences or alternative sequences if |
is used inside the groups) of a comma and 0+ word chars\)
- a literal )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With