如何使用正则表达式过滤无效文件名字符的字符串
How to filter a string for invalid filename characters using regex
我的问题是我不希望用户输入任何错误的内容,所以我试图删除它,我的问题是我制作了一个正则表达式,它删除了除单词之外的所有内容,并且还删除了 . , - 但我需要这些标志来让用户满意 :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'
为确保文件名有效,您应该使用 GetInvalidFileNameChars .NET 方法检索所有无效字符并使用正则表达式检查文件名是否有效:
[regex]$containsInvalidCharacter = '[{0}]' -f ([regex]::Escape([System.IO.Path]::GetInvalidFileNameChars()))
if ($containsInvalidCharacter.IsMatch(($ResearchGroupInbox.Text)))
{
# filename is invalid...
}
$ResearchGroupInbox.Text -replace '~|"|#|%|\&|\*|:|<|>|\?|\/|\|{|\||}'
或者按照 @Wiketor
建议您可以避免 '[~"#%&*:<>?/\{|}]+'
请注意,如果您想替换无效的文件名字符,您可以利用 How to strip illegal characters before trying to save filenames?
中的解决方案
回答你的问题,如果你有特定的字符,将它们放入字符 class,不要使用通用的 \W
,它也匹配更多的字符。
使用
[~"#%&*:<>?/\{|}]+
请注意,除了 \
之外的所有这些字符都不需要在字符 class 内转义。此外,添加 +
量词(匹配 1 次或多次出现的量化子模式)简化了替换过程(匹配整个连续的字符块并用替换模式一次替换所有字符(此处为空字符串)) .
请注意,您可能还需要考虑 con
、lpt1
等文件名。
我的问题是我不希望用户输入任何错误的内容,所以我试图删除它,我的问题是我制作了一个正则表达式,它删除了除单词之外的所有内容,并且还删除了 . , - 但我需要这些标志来让用户满意 :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'
为确保文件名有效,您应该使用 GetInvalidFileNameChars .NET 方法检索所有无效字符并使用正则表达式检查文件名是否有效:
[regex]$containsInvalidCharacter = '[{0}]' -f ([regex]::Escape([System.IO.Path]::GetInvalidFileNameChars()))
if ($containsInvalidCharacter.IsMatch(($ResearchGroupInbox.Text)))
{
# filename is invalid...
}
$ResearchGroupInbox.Text -replace '~|"|#|%|\&|\*|:|<|>|\?|\/|\|{|\||}'
或者按照 @Wiketor
建议您可以避免 '[~"#%&*:<>?/\{|}]+'
请注意,如果您想替换无效的文件名字符,您可以利用 How to strip illegal characters before trying to save filenames?
中的解决方案回答你的问题,如果你有特定的字符,将它们放入字符 class,不要使用通用的 \W
,它也匹配更多的字符。
使用
[~"#%&*:<>?/\{|}]+
请注意,除了 \
之外的所有这些字符都不需要在字符 class 内转义。此外,添加 +
量词(匹配 1 次或多次出现的量化子模式)简化了替换过程(匹配整个连续的字符块并用替换模式一次替换所有字符(此处为空字符串)) .
请注意,您可能还需要考虑 con
、lpt1
等文件名。