vb.net 正则表达式删除所有数字和所有点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23202384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:26:26  来源:igfitidea点击:

Regular Expression to remove all numbers and all dots

regexvb.netnumbersextract

提问by eawedat

I have this code in VB.NET :

我在 VB.NET 中有这个代码:

MessageBox.Show(Regex.Replace("Example 4.12.0.12", "\d", ""))

It removes/extracts numbers

它删除/提取数字

I want also to remove dots

我也想删除点

so I tried

所以我试过了

MessageBox.Show(Regex.Replace("Example 4.12.0.12", "\d\.", ""))

but it keeps the numbers.

但它保留了数字。

how to remove both (numbers & dots) from the string ?

如何从字符串中同时删除(数字和点)?

thanks.

谢谢。

回答by pmcoltrane

Try using a character group:

尝试使用字符组:

MessageBox.Show(Regex.Replace("Example 4.12.0.12", "[\d\.]", ""))

I'll elaborate since I inadvertently posted essentially the same answer as Steven.

我会详细说明,因为我无意中发布了与史蒂文基本相同的答案。

Given the input "Example 4.12.0.12"

鉴于输入 "Example 4.12.0.12"

  • "\d"matches digits, so the replacement gives "Example ..."
  • "\d\."matches a digit followed by a dot, so the replacement gives "Example 112"
  • "[\d.]"matches anything that is a digit or a dot. As Steven said, it's not necessary to escape the dot inside the character group.
  • "\d"匹配数字,所以替换给出 "Example ..."
  • "\d\."匹配一个数字后跟一个点,所以替换给出 "Example 112"
  • "[\d.]"匹配任何数字或点。正如史蒂文所说,没有必要对字符组内的点进行转义。

回答by Steven Doggart

You need to create a character group using square brackets, like this:

您需要使用方括号创建一个字符组,如下所示:

MessageBox.Show(Regex.Replace("Example 4.12.0.12", "[\d.]", ""))

A character group means that any one of the characters listed in the group is considered a valid match. Notice that, within the character group, you don't need to escape the .character.

字符组意味着组中列出的任何一个字符都被视为有效匹配。请注意,在字符组内,您不需要对.字符进行转义。