正则表达式替换字符串中的非单词字符,忽略特定字符
在 VB.net 中,我有以下行从字符串中删除所有非字母数字字符:
return Regex.Replace(build, "[\W]", "")
我现在需要扩展它以删除不是 [] 或 _ 的非字母数字字符。
我已将行更改为:
return Regex.Replace(build, "[\W\[\]_]", "")
但是我很确定这表示
替换非单词或 [ 或 ] 或 _
如何否定 [] 和 _ 字符的测试,以便它显示
replace non-word and not [ and not ] and not _
一些示例:(
"[Foo Bar_123456]" => "[FooBar_123456]"
"[Foo Bar_123-456*]" => "[FooBar_123456]"
如果需要,可以提供更多)
In VB.net I've got the following line that removes all non-alphanumeric chars from a string:
return Regex.Replace(build, "[\W]", "")
I now need to extend this to remove non-alphanumeric chars that aren't [] or _.
I've changed the line to:
return Regex.Replace(build, "[\W\[\]_]", "")
However I'm pretty sure that this says
replace non-word or [ or ] or _
how do I negate the tests for the [] and _ chars so that it says
replace non-word and not [ and not ] and not _
Some examples:
"[Foo Bar_123456]" => "[FooBar_123456]"
"[Foo Bar_123-456*]" => "[FooBar_123456]"
(More can be supplied if necessary)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个正则表达式:
它表示匹配(然后替换)除字母数字(包括“_”)、“[”和“]”之外的所有内容
Try this regex:
It says match (and then replace) everything except alphanumeric (including "_"), "[", and "]"
您可以使用字符集减法:
即。
\W
并删除了_
、[
和]
(后两个需要转义,因为它们是字符集)。逐字字符串@"..."
避免了需要在 C# 中转义每个反斜杠:You can use character set subtraction:
Ie.
\W
with_
,[
and]
removed (latter two needing an escape because they are meta-characters in a character set). A verbatim string@"..."
avoids needing to escape each backslash from C#: