re = /(?x)
.+(?:Sub|Function)\ (\w+)\( # method declaration
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
(?: # start of parameter pattern
[\w\W]*?(?<=\b) # parameter may be preceded by ByVal or Optional keywords
([a-z]\w+)+ # parameter starts with a lower case
.*? # parameter may be followed by its type declaration
(?:=\ ([^\,]+)){0,1} # optional parameters may be followed by default value, it is captured to be used in method call
(?:,|\)) # parameter is followed by , or )
){0,1} # end of parameter pattern (may be copy-pasted infinitely to capture all parameters)
/m
str = 'Public Sub FindDictTreeNodes(ByVal startAtNodeDict As Scripting.Dictionary, ByVal searchKey As Variant, ByVal searchKeyItem As Variant, _
ByVal result As Collection, Optional ByVal childrenKey As Variant = "Children", Optional ByVal findOnlyFirst As Boolean, _
Optional ByVal searchAtLevel As Integer = 0, Optional ByVal useLikeOperatorForMatching As Boolean = False, _
Optional ByVal nowAtLevel As Integer)
'
subst = '#CSV list of parameters\\n${2}, ${4}, ${6}, ${8}, ${10}, ${12}, ${14}, ${16}, ${18}, ${20}, ${22}, ${24}, ${26}, ${28}, ${30}\\n\\n#vertical list of parameters\\n\'${2} - \\n\'${4} - \\n\'${6} - \\n\'${8} - \\n\'${10} - \\n\'${12} - \\n\'${14} - \\n\'${16} - \\n\'${18} - \\n\'${20} - \\n\'${22} - \\n\'${24} - \\n\'${26} - \\n\'${28} - \\n\'${30} - \\n'
result = str.gsub(re, subst)
# Print the result of the substitution
puts result
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Ruby, please visit: http://ruby-doc.org/core-2.2.0/Regexp.html