I need to parse php docComment annotations with regexp. Possible annotations:
/**
* @Annotation()
* @Annotation\Name("var1", "var2")
* @Annotation(["var1", "var2"], "var3")
* @Annotation\Filter\Name(["var1", "var2"], "var3", {"var4": "var5"})
*/
Basically @Annotation(<json>). In php I decode it like this json_decode('['.$json.']')
Now I use regexp /@(\w+[\\\\\w+]*)\(([^\)]*)?\)/ but this is not always true. For example:
@Annotation("name()")
What regexp should I use?
Is it possible to parse more than one annotation in the line?
/**
* @a() @b("var1")
* ....
*/
UPDATE:
How to write this in regexp:
# match annotation name
# if brackets are empty
# match ()[space|newline]
# (else) if something is in brackets
# match everything until:
# ")[space|newline]
# or })[space|newline]
# or ])[space|newline]
UPDATE 2
What about this regexp @((\w+)([\\\\\w+]*))(?:\(\s*\)|\((.*?["\}\]])(?:\s|$)*\))(?:\s|$). Can it be simplified?
demo
Modify it to:
@([\\\w]+)\((.*)\)
Live demo #1
Update 3
For more annotations in one line:
@([\\\w]+)\((.*?)\)(?:\s|$)
Live demo #2
Update 4
Based on your last edit:
@([\\\w]+)\((?:|(.*?[]"}]))\)
Live demo #3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With