1

I tried various combinations but unsuccesfull at figuring out correct regex pattern.

Basically I want to capture patterns like examples below:

  1. {{variable}}
  2. {{variable.function1{param1}}}
  3. {{variable.function1{param1}.function2{param2}}}

and so on..

I wanted to capture variable,function1,param1,function2,param2 from this

So far I have below regex which does not work completely

\{\{([^{}.]+)(\.([^{}]+)\{([^{}]+)\})*\}\}

If I try to apply above pattern on example 3, I get below groups

  • Group#1 - variable
  • Group#2 - .function2{param2}
  • Group#3 - function2
  • Group#4 - param2

I was expecting somthing as below,

  • Group#1 - variable
  • Group#2 - .function1{param1}
  • Group#3 - function1
  • Group#4 - param1
  • Group#5 - .function2{param2}
  • Group#6 - function2
  • Group#7 - param2

PS: you can check without writing code at http://regexr.com/3e4st

Amit
  • 797
  • 9
  • 32
  • 1
    I would recommend using https://regex101.com/ instead as they actually explain why it wasn't doing what you think it would do (you needed to have brackets around the second portion as well `( (\.([^{}]+)\{([^{}]+)\})* )` vs `(\.([^{}]+)\{([^{}]+)\})*` ) As for the actual answer, it's still not correct, so I'll have a look, but no promises! – A. L Aug 31 '16 at 06:15
  • Thanks @A.lau please let me know if you find, tx – Amit Aug 31 '16 at 06:18

2 Answers2

1

Okay, so the reason why your thing doesn't work, is because you're basically only capturing one instance of the thing in general, which means each capture group can only return one instance of what you want. So what's needed is the global variable or your equivalent in whatever language you're using.

Example: https://regex101.com/r/pO8xN2/3

A. L
  • 11,695
  • 23
  • 85
  • 163
1

The number of groups in a regex match is fixed. See an older post of mine with more explanation. In your case that number is 4.

When a group matches repeatedly, you will usually only be able to access the value of the last occurrence in the string. That's what you see with your issue: function2, param2.

Some regex engines allow accessing previous group captures (for example the one in .NET). The majority don't. Whether you can solve your issue easily or not strictly depends on your regex engine.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • You are right! i cannot do all group captures in one go. However since A. Lau answer gave me more insight into solving my problem, i will accept that answer. Thanks for your response Tomalak. – Amit Aug 31 '16 at 20:47