- function Add-PubText {
- param (
- [ValidateNotNullOrEmpty()]
- [string]$text
- )
- $reMethod = [regex]'(?ms)^(?>\s*)public\b[^(]*\b\w+\b(?>\s*)\((?>[^)]*)\)(?>[^{]*)(?<o>\{)(?>/\*.*?\*/|//[^\n]*|"(?>""|\\"|[^"])*"|(?<o>\{)|(?<-o>\})|.)+?(?(o)!)\s*'
- $ms = $reMethod.Matches($text)
- if ($ms.Count -eq 0) { return '' }
- $prefix = $text.Substring(0, $ms[0].Index)
- $suffix = $text.Substring($ms[0].Index)
- if ($prefix -match '\S') {
- $prefix = @"
- public static void Main()
- {
- $prefix
- }
- "@
- return $prefix + $suffix
- }
- $t = $ms[$ms.Count - 1].Index + $ms[$ms.Count - 1].Length
- $prefix = $text.Substring(0, $t)
- $suffix = $text.Substring($t)
- if ($suffix -match '\S') {
- $suffix = @"
- public static void Main()
- {
- $suffix
- }
- "@
- return $prefix + $suffix
- }
- return ''
- }
- Add-PubText $s1
- Add-PubText $s2
复制代码
|