# Some examples and assuming that the annotation you want to parse
# starts with a /** and ends with a */. This may be spread over
# several lines.
text = """
/**
@Title(value='Welcome', lang='en')
@Title(value='Wilkommen', lang='de')
@Title(value='Vitajte', lang='sk')
@Snippet
,*/
class WelcomeScreen {}
/** @Target("method") */
class Route extends Annotation {}
/** @Mapping(inheritance = @SingleTableInheritance,
columns = {@ColumnMapping('id'), @ColumnMapping('name')}) */
public Person {}
"""
text2 = """ /** * comment */
CLASS MyClass extens Base {
/** * comment */
public function xyz
"""
import re
# Match a PHP annotation and the word following class or public
# function.
annotations = re.findall(r"""/\*\* # Starting annotation
#
(?P<annote>.*?) # Namned, non-greedy match
# including newline
#
\*/ # Ending annotation
#
(?:.*?) # Non-capturing non-greedy
# including newline
(?:public[ ]+function|class) # Match either
# of these
[ ]+ # One or more spaces
(?P<name>\w+) # Match a word
""",
text + text2,
re.VERBOSE | re.DOTALL | re.IGNORECASE)
for txt in annotations:
print("Annotation: "," ".join(txt[0].split()))
print("Name: ", txt[1])