正则表达式替换问题 [英] Regular Expression Replace Issue

查看:96
本文介绍了正则表达式替换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了一些代码来确保无效的特殊字符不会将其放入Windows窗体项目的文本框中.用户似乎经常将信息从Microsoft Word复制/粘贴到此文本框中,当他们粘贴Word自动更改的项目符号或字符(例如连字符和省略号)时,这已成为一个问题.它在开发中运行良好,在测试中我们没有问题.现在,我有一个能够将项目符号粘贴到其中的客户.它不会引起任何错误,但不会像我期望的那样将项目符号转换为星号.

有问题的客户以非常严格的安全性而闻名.这种情况使我想到了一些防病毒应用程序将如何阻止SendKeys在程序中工作.有谁知道这是否也对使用正则表达式的.Replace这样的问题有影响?如果是这样,是否有其他替代方法可以实现我的最终目标?还是有人可以找到一个行不通的原因?

以下是我仅在文本框中保留有效字符的事件和方法:

I have a bit of code I use to make sure that invalid special characters do not make it into a textbox in a windows form project. Users often seem to copy/paste info into this textbox from Microsoft Word and it was becoming a problem when they would paste bullets or characters that Word auto-changes like hyphens and elipses. It works fine in development and we had no problems with it in testing. Now I have a customer who is able to get bullets to paste into it. It doesn''t cause any errors, but it doesn''t convert the bullet to an asterisk like I''m expecting it to.

The customer in question is known for having very tight security. The situation made me think about how some anti-virus applications will block SendKeys from working in programs. Does anyone know if this is also an issue for things like a .Replace using regular expressions? If so, are there any alternative methods to accomplish my end goal? Or can anyone find a reason why this wouldn''t work?

Here are the event and method I use to keep only valid characters in the textbox:

 Private Sub txtSpecialReport_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSpecialReport.TextChanged
     'Only allow known characters to be pasted or typed into txtSpecialReport to avoid Conversion Error on characters
     'that are okay in access db but not on iSeries when claim is uploaded.
     Dim intStart As Integer = txtSpecialReport.SelectionStart
     '^negates the whole thing (we want ones that DON'T match)
     '\w is for a-zA-Z0-9
     '\s is for spaces, tabs, returns
     '\!\@\#\$\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\"" are allowed special characters
     Dim strPattern As String = "[^\w\s\!\@\#\$\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\""]"
     Dim myRegex As New System.Text.RegularExpressions.Regex(strPattern)
     txtSpecialReport.Text = myRegex.Replace(txtSpecialReport.Text, New System.Text.RegularExpressions.MatchEvaluator(AddressOf ReplaceKnownSpecials))
     txtSpecialReport.SelectionStart = intStart
 End Sub

 Private Function ReplaceKnownSpecials(ByVal match As System.Text.RegularExpressions.Match) As String
    Select Case Asc(match.Value(0))
        Case 63, 146
            Return "'" 'Replace an ascii 63 or 146 with ascii 39
        Case 133
            Return "." 'Replace an ascii 133 elipses with a single period (Not three (...) because then it changes string length)
        Case 150
            Return "-" 'Replace an ascii 150 dash with an ascii 45 dash
    End Select
    Return "*" 'Relace any other unknown char with an asterisk
End Function

推荐答案

\%\ ^ \& \ * \(\)\-\ = \ + \ [\] \ {\} \\\ \ | \; \:\'\,\< \.\> \/\?\"被允许使用特殊字符 Dim strPattern As 字符串 = "
\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\"" are allowed special characters Dim strPattern As String = "[^\w\s\!\@\#\


\%\ ^ \& \ * \(\)\-\ == ++ [[\] \ {\} \\\ | \; \:\:\'\,\< \.\> \/\?\" ]" Dim myRegex As New System.Text. RegularExpressions.Regex(strPattern) txtSpecialReport.Text = myRegex.Replace(txtSpecialReport.Text,新建 System.Text.RegularExpressions.MatchEvaluator( AddressOf ReplaceKnownSpecials)) txtSpecialReport.SelectionStart = intStart 结束 私有 功能 ReplaceKnownSpecials(> ByVal 匹配 As System.Text.RegularExpressions.Match) As 字符串 选择 案例 Asc(match.Value( 0 )) 案例 63 146 返回 " ' 将ascii 63或146替换为ascii 39 案例 133 返回 " ' 用一个句号替换ASCII 133椭圆(不是三个(...),因为这会改变字符串的长度) 案例 150 返回 " ' 用ascii 45破折号替换ascii 150破折号 结束 选择 返回 " ' 使用星号分隔其他任何未知字符 结束 功能
\%\^\&\*\(\)\-\=\+\[\]\{\}\\\|\;\:\'\,\<\.\>\/\?\""]" Dim myRegex As New System.Text.RegularExpressions.Regex(strPattern) txtSpecialReport.Text = myRegex.Replace(txtSpecialReport.Text, New System.Text.RegularExpressions.MatchEvaluator(AddressOf ReplaceKnownSpecials)) txtSpecialReport.SelectionStart = intStart End Sub Private Function ReplaceKnownSpecials(ByVal match As System.Text.RegularExpressions.Match) As String Select Case Asc(match.Value(0)) Case 63, 146 Return "'" 'Replace an ascii 63 or 146 with ascii 39 Case 133 Return "." 'Replace an ascii 133 elipses with a single period (Not three (...) because then it changes string length) Case 150 Return "-" 'Replace an ascii 150 dash with an ascii 45 dash End Select Return "*" 'Relace any other unknown char with an asterisk End Function


我从未听说过正则表达式被阻止:我不相信它们可能是-所有它们都是一个可更改文本字符串的函数.

您确定他们粘贴了非法字符吗?是不是像(例如)空格点空间还是类似的?
I''ve never heard of regexes being blocked: I''m not convinced they could be - all they are is a function that changes text strings.

Are you sure they are pasting illegal characters? It''s not coming is as (say) space-dot-space or similar?


这篇关于正则表达式替换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆