regex - Parse string in PowerShell to create output using variables -


i want create little script takes particular type of string , uses in way can process things easily

  • %1g.%s means 1 char given name , full secondary name (output should j.snow built 2 parameters)
  • %g.%s means full given name dot , full secondary name (output should john.snow built 2 parameters)
  • %5g.%s means 5 chars given name , full secondary name if given name shorter use shorter version (john.snow)
  • %g%s given givenname , secondary name without dot (johnsnow)
  • etc

question how start processing don't create monster if/else cases? or should that?

edit. since microsoft exchange email template behaviour wanted explain office 365 without on-premise option doesn't have email templates option. in other words want create script mimic behaviour in way.

$firstname = "alice" $lastname = "bloggs"  $template = '%2g.%s@example.com'  function get-namesection {     # returns first $num characters of name     # unless $num 0, missing or longer name     # returns entire name      param([string]$name, [int]$num)      if (-not $num -or $num -gt $name.length) {          $name      } else {         $name.substring(0, $num)     } }  $template = [regex]::replace($template, '%(\d*)g', {param($m) get-namesection $firstname $m.groups[1].value }) $template = [regex]::replace($template, '%(\d*)s', {param($m) get-namesection $lastname $m.groups[1].value })  $template 

Comments