How can I recursively match a pattern using Regular Expressions?

后端 未结 5 1802
小蘑菇
小蘑菇 2020-12-04 01:36

The string can be like one of the following:

a(b,c)
a(a(b,c),d)
a(a(a(a(a(b,c),d),a(e,f)),g),h)
etc

I want to match an unlimited number of

5条回答
  •  萌比男神i
    2020-12-04 02:12

    Java's standard regex lib does not support recursion, so you can't match such general nested constructs with it.

    But in flavors that do support recursion (Perl, PCRE, .NET, etc) you can use expressions like:

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

提交回复
热议问题