5

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?

Chirag Parmar
  • 833
  • 11
  • 26
Constantine
  • 1,802
  • 3
  • 23
  • 37
  • 3
    `[,]{2,}` Two or more consecutive commas. See [Java RegEx Quantifiers](https://docs.oracle.com/javase/tutorial/essential/regex/quant.html). – nbrooks Nov 17 '16 at 07:32

1 Answers1

2

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 )
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • (?:...) - specific for Java? Can't find any doc for it. – Constantine Nov 17 '16 at 09:52
  • 1
    See [What is a non-capturing group? What does a question mark followed by a colon (?:) mean?](http://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-a-question-mark-followed-by-a-colon). Also, [Non-Capturing groups](http://www.regular-expressions.info/brackets.html#noncap). It is a widely accepted construct in NFA regex (non-POSIX one). – Wiktor Stribiżew Nov 17 '16 at 09:56