2

I am attempting to make a regex to recongise a complex script declaration such as:

script void bar(type foo, type baz)

Where both "type"'s are in the same capture group

currently, I have the Regex:

(script)\s(\w*)\s(\w*)\((?:([a-z|A-Z]+)\s\w+)?\)

This recognises capture groups, as shown in the link. https://regex101.com/r/nB3oL3/3

  1. [0-6] script
  2. [7-11] void
  3. [12-15] bar
  4. [16-20] type

It also recognises the strings

script void bar(type foo)
script void bar()

Which is exactly what I want, except for the fact that it will not recognise any additional parameters such as "type baz"

script void bar(type foo, type baz)

I'm struggling to work out a way of recursively recognising the "type" in

", type baz"
addition without adding additional capture groups (if this is even possible?). I was also unsure if I needed to use a branch reset for this effect?

As i'm doing this for sublime, I figure it must be using the python regex engine.

Any help would be much appriciated!

  • Chris
Chris
  • 97
  • 10
  • **A quick note - ive just noticed that this only recognises lower case, this was not my intention, but easily fixable** – Chris Mar 03 '15 at 14:32

1 Answers1

1

In an engine that supports recursive regex, ^(script)\s(\w+)\s(\w+)\((?:([a-zA-Z]+)\s\w+(?:,\s)?)*\)$ should work, but as taken from this post, recursive implementations is not widely implemented. ( As the comment on this state, there exists for example plug-ins for python to support this. )

Community
  • 1
  • 1
Heinrich Henning
  • 933
  • 5
  • 15
  • This is good, but seems to only capture the last entered type – Chris Mar 03 '15 at 15:02
  • `script void bar(type foo, ` **type** `baz)` rather than both of them - or is the website an example of the lack of support for recursive regex? – Chris Mar 03 '15 at 15:03
  • From what I could infer the website is an example of the lack of support, as soon as I get a chance I will install the python plug-in and try directly in the interpreter. – Heinrich Henning Mar 03 '15 at 16:25