我如何处理vb.net中的特殊字符?

时间:2023-01-13 22:01:36

special character set: `~!@#$%^&*()_-+={}[]\|:;""'<>,.?/

特殊字符集:“~ ! @ # $ % ^ & *()_ + = { }[]\ |:;”“< >,。/

Is this the right way to search for items within that special character set?

这是在特殊字符集中搜索条目的正确方法吗?

    Regex.IsMatch(Result.Text, "^[`-/]")

I always get an error.... Do I need to use ASCII codes? If so, how can I do this? Thank you in advance!

我总是得到一个错误....我需要使用ASCII码吗?如果是的话,我怎么做呢?提前谢谢你!

3 个解决方案

#1


2  

Regular expressions have a different escape syntax and rules than VB.NET. Since you're dealing with a regex string in your code, you have to make sure the string is escaped properly for regex and VB.NET.

正则表达式的转义语法和规则与VB.NET不同。由于您正在处理代码中的regex字符串,所以必须确保该字符串为regex和VB.NET正确地转义。

In your example, the - needs to be escaped with a ...

在你的例子中,-需要用一个…

Regex.IsMatch(Result.Text, "^[`\-/]")

To match any character in the provided string, try this...

要匹配所提供的字符串中的任何字符,请尝试…

Regex.IsMatch(Result.Text, "[`~!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\\\|:;""'<>,\.\?/]")

#2


1  

Try this:

试试这个:

[`~!@#$%^&*()_+={}\[\]\\|:;""'<>,.?/-]

Working regex example:

工作的正则表达式的例子:

http://regex101.com/r/aU3wO9

http://regex101.com/r/aU3wO9

In a char set, only some of them need to be escaped. like ] and \ and if - is at the end it doesn't need to be escaped. I'm not sure about [ so I just escaped it anyway.

在char集中,只需要转义其中的一部分。就像]和“如果-是”,最后它不需要被转义。我不太确定,所以我还是逃了出来。

#3


0  

You can use an escape character in the string where there is a special character

您可以在有特殊字符的字符串中使用转义字符

Dim str() As Byte
Dim j, n As Long
Dim ContainsSpecialChar As Boolean
Dim TempVendorName As String
str = Trim(VendorName)
n = 1

For j = 0 To UBound(str) - 1 Step 2
    If (str(j) > 32 And str(j) < 47) Or (str(j) > 57 And str(j) < 65) Or (str(j) > 90 And str(j) < 97) Or (str(j) > 122) Then
         ContainsSpecialChar = True
         TempVendorName = Left(VendorName, n - 1) + "\" + Mid(VendorName, n)
         n = n + 1
    End If
    n = n + 1
Next

#1


2  

Regular expressions have a different escape syntax and rules than VB.NET. Since you're dealing with a regex string in your code, you have to make sure the string is escaped properly for regex and VB.NET.

正则表达式的转义语法和规则与VB.NET不同。由于您正在处理代码中的regex字符串,所以必须确保该字符串为regex和VB.NET正确地转义。

In your example, the - needs to be escaped with a ...

在你的例子中,-需要用一个…

Regex.IsMatch(Result.Text, "^[`\-/]")

To match any character in the provided string, try this...

要匹配所提供的字符串中的任何字符,请尝试…

Regex.IsMatch(Result.Text, "[`~!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\\\|:;""'<>,\.\?/]")

#2


1  

Try this:

试试这个:

[`~!@#$%^&*()_+={}\[\]\\|:;""'<>,.?/-]

Working regex example:

工作的正则表达式的例子:

http://regex101.com/r/aU3wO9

http://regex101.com/r/aU3wO9

In a char set, only some of them need to be escaped. like ] and \ and if - is at the end it doesn't need to be escaped. I'm not sure about [ so I just escaped it anyway.

在char集中,只需要转义其中的一部分。就像]和“如果-是”,最后它不需要被转义。我不太确定,所以我还是逃了出来。

#3


0  

You can use an escape character in the string where there is a special character

您可以在有特殊字符的字符串中使用转义字符

Dim str() As Byte
Dim j, n As Long
Dim ContainsSpecialChar As Boolean
Dim TempVendorName As String
str = Trim(VendorName)
n = 1

For j = 0 To UBound(str) - 1 Step 2
    If (str(j) > 32 And str(j) < 47) Or (str(j) > 57 And str(j) < 65) Or (str(j) > 90 And str(j) < 97) Or (str(j) > 122) Then
         ContainsSpecialChar = True
         TempVendorName = Left(VendorName, n - 1) + "\" + Mid(VendorName, n)
         n = n + 1
    End If
    n = n + 1
Next