Home > Blockchain >  PowerShell WinForms textbox, prevent unwanted regex pattern characters
PowerShell WinForms textbox, prevent unwanted regex pattern characters

Time:04-09

In PowerShell I've been trying different ways of stripping out all characters from a WinForms textbox other than the pattern I need and I cannot come up with a better solution for what I'm after; I am learning regex and need some guidance. This feels basic to me but I feel like I'm missing something obvious. I'm using the textbox's TextChanged event to prevent illegal characters from being typed and I have (what feels like) a bloated solution.

Goal:

  • User allowed to type a single leading dollar sign followed by up to 6 chars from [A-V0-9]
    • If no $ is typed, the first legal character typed should produce a leading $ followed by said legal character (typing A will produce $A in the textbox)
    • If an illegal character is typed first, no leading $ should be produced in the textbox
  • Illegal characters won't show up in the textbox
  • Pasted text is processed/stripped as efficiently as possible
  • The last character deleted will also delete the remaining $
    • If textbox shows $T and user backspaces the T leaving a single $, the remaining $ will also be deleted


Pasting into the textbox examples:

  • $$-#AZY$B9=% would become $AB9
  • Z))
  • Related