Regular Expressions 101

Community Patterns

golang const|func|type declaration matcher

0

Regular Expression
PCRE (PHP <7.3)

/
(?x) # Set intent level (?(DEFINE)(?<indent>[[:space:]]{4})) # Must start at new line ^ # Captures following statement's description comment lines if written (?<statementDescComment> (?: # Repeat below until next line not comment (?=(?P>indent)\/)\V*\v )+ )? # Generic Statement Capture (?<statement> # Const declaration capture (?<const> (?P>indent)const[[:space:]]*\([[:space:]]* \v (?: (?!(?P>indent)\))\V*\v )* (?P>indent)\) ) | # Full function declaration capture (?<func> # Function declaration initialization (?<funcDecLine> (?P>indent)func\V+ )\v # Repeat subcapture until line starting with close bracket (?: (?!(?P>indent)\})\V*\v )* # Closing bracket (?<funcCloseBracket>(?P>indent)\}) ) | (?<type> # Type Declaration # Check before wasting time (?=(?P>indent)type) # Short Form (?<typeDecShort> (?P>indent)type\V+[^\{]\v ) | # Longer Form (Brackted) (?<typeDecLong> (?P>indent)type\V+ )\v # Repeat subcapture until line starting with close bracket (?: (?!(?P>indent)\})\V*\v )* # Closing bracket (?<typeCloseBracket>(?P>indent)\}) ) )
/
gm

Description

Intended for commenting out a source file in a more targeted/formatted manner—makes folding and navigation easiers while annotating (substitution is wrapping each statement in block comments.)

Submitted by anonymous - 4 years ago