Regular Expressions 101

Community Patterns

PHPDoc tags extraction

0

Regular Expression
PCRE (PHP <7.3)

~
(?<=^|\v) \h*+\*\h*+ (?: (?: (?P<annotation> @(?P<name>\w++) (?: (?<=@param)\h++(?P<type>\w++(?:\[\])?)\h++\$(?P<variable>\w++) |(?<=@return)\h++(?P<type>\w++(?:\[\])?) |(?<=@throws)\h++(?P<class>\w++) |(?<=@cache)\h++(?P<time>\d++) |(?<=@roles)\h++(?P<roles>\w++(?:\h*+,\h*+\w++)*) )? ) ) (?P<comment>\h++[^\v]++)? (?:(?P<comment>\v)\h*+\*\h*+(?P<comment>[^\v@\h\*][^\v]++))*+ |(?P<description>(?:[^\v@\h\*][^\v]++) (?: \v\h*+\*\h*+[^\v@\h\*][^\v]++ )*+ ) )
~
gxsJ

Description

Extract descriptions and tags in a PHPDoc comment bloc

Some tags are customized for my usage, but can be removed or replaced.

/!\ multiline descriptions must be cleaned after capture to remove the spaces and '*' at the start of each line after the first.

Submitted by olivier.parmentier - 6 years ago