【发布时间】:2016-07-25 19:24:24
【问题描述】:
我的问题是我不希望用户输入任何错误,所以我试图删除它,我的问题是我制作了一个正则表达式,它删除了除单词之外的所有内容,并且还删除了 。 , - 但我需要这些标志来让用户开心:D
简短总结:此脚本使用正则表达式删除输入字段中的坏字符。
输入栏:
$CustomerInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$CustomerInbox.Location = New-Object System.Drawing.Size(10,120) #Location -> where the label is located in the window
$CustomerInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$CustomerInbox.MaxLength = 30 #sets max. length of the input box to 30
$CustomerInbox.add_TextChanged($CustomerInbox_OnTextEnter)
$objForm.Controls.Add($CustomerInbox) #adding -> adds the input box to the window
功能:
$ResearchGroupInbox_OnTextEnter = {
if ($ResearchGroupInbox.Text -notmatch '^\w{1,6}$') { #regex (Regular Expression) to check if it does match numbers, words or non of them!
$ResearchGroupInbox.Text = $ResearchGroupInbox.Text -replace '\W' #replaces all non words!
}
}
我不想出现的坏字符:
~ " # % & * : < > ? / \ { | } #those are the 'bad characters'
【问题讨论】:
-
如果你有特定的字符,把它们放到一个字符类中,为什么要使用一个通用的
\W?使用[~"#%&*:<>?/\\{|}]。但是,您还应该考虑con、lpt1等文件名。 -
好吧忘记它现在工作了谢谢你的支持:D
-
然后我添加了评论作为答案。
标签: regex powershell powershell-2.0