Regexp specify counter in character class

I am trying to create regexp to search for duplicate commas, for example here:

baz(uint32,,bool) 

Now my pattern is: \w*\([\w\[\],?+]+\)

which does not work.

How to specify the number of elements in a character class?

+5
source share
1 answer

You cannot specify the number of occurrences within a character class, as this construct is used to define a specific type of character. Inside [...] * , + ? , {1,2} treated as literals.

If you just need to combine multiple comma separated words in parentheses, use

 \w*\(\w*(?:,\w*)*\) 

or with the required first word:

 \w+\(\w*(?:,\w*)*\) ^ 

See regex demo (or this one ).

In Java, use String re = "\\w+\\(\\w*(?:,\\w*)*\\)"; .

Template Details :

  • \w* - characters + + +
  • \( - one literal (
  • \w* - characters + + +
  • (?:,\w*)* - zero or more sequences ( (?:...) and (...) define sequences or alternative sequences, if | used inside groups) with a comma and 0+ characters
  • \) - letter )
+2
source

Source: https://habr.com/ru/post/1259880/


All Articles