.net Regex.替换不排除插入符号字符



我正在尝试使用 .NET Regex.Replace (VB.NET 中的示例)从字符串中排除所有非字符和空格。例如,在下面的字符串 s 中,我认为模式 [^A-z ] 应该删除除空格之外的所有非字母。但是,这似乎行不通。我做错了什么?

Sub Try_Regex_Remove_Caret_Symbol()
' ^ (caret) character is not being removed via exclusion
Dim s As String, p As String
s = "I have a caret which I want removed ^$@#!&"
p = "[^A-z ]"
Console.WriteLine("Input : " & s)
Console.WriteLine("Output: " & Regex.Replace(s, p, ""))
' Input : I have a caret which I want removed ^$@#!&
' Output: I have a caret which I want removed ^
' Note that the caret (^) is not removed as expected
End Sub

这是 ascii 表

0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
2     !  "  #  $  %  &  '  (  )  *  +  ,  -  .  /
3  0  1  2  3  4  5  6  7  8  9  :  ;  <  =  >  ?
4  @  A  B  C  D  E  F  G  H  I  J  K  L  M  N  O
5  P  Q  R  S  T  U  V  W  X  Y  Z  [    ]  ^  _
6  `  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o
7  p  q  r  s  t  u  v  w  x  y  z  {  |  }  ~  

您会注意到插入符号 (^) 位于 A 和 z 之间。要获得所需的效果,您需要:

p = [^A-Za-z ]

使用以下正则表达式进行替换:

[^A-Za-z ]

问题是插入符号位于范围A-Za-z之间,因此您将其排除在替换为[^A-z]之外。

完整代码:

s = "I have a caret which I want removed ^$@#!&"
p = "[^A-Za-z ]"
Console.WriteLine("Input : " & s)
Console.WriteLine("Output: " & Regex.Replace(s, p, ""))

在这里演示:

Rextester

相关内容

  • 没有找到相关文章

最新更新