Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regexp specify counter in character class

Tags:

java

regex

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?

like image 803
Constantine Avatar asked Oct 29 '22 16:10

Constantine


1 Answers

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 )
like image 189
Wiktor Stribiżew Avatar answered Nov 09 '22 14:11

Wiktor Stribiżew