C# 如何筛选出某些组合?

C# 如何筛选出某些组合?,c#,regex,wpf,filter,C#,Regex,Wpf,Filter,我正在尝试使用正则表达式过滤文本框的输入。小数点前最多需要三个数字,小数点后需要两个数字。这可以是任何形式 我尝试过改变regex命令,但它会产生错误,单个输入无效。我在WPF中使用一个文本框来收集数据 bool containsLetter = Regex.IsMatch(units.Text, "^[0-9]{1,3}([.] [0-9] {1,3})?$"); if (containsLetter == true) { MessageBox.Show("

我正在尝试使用正则表达式过滤文本框的输入。小数点前最多需要三个数字,小数点后需要两个数字。这可以是任何形式

我尝试过改变regex命令,但它会产生错误,单个输入无效。我在WPF中使用一个文本框来收集数据

bool containsLetter = Regex.IsMatch(units.Text, "^[0-9]{1,3}([.] [0-9] {1,3})?$");
if (containsLetter == true)
{
    MessageBox.Show("error");
}
return containsLetter;
我希望正则表达式过滤器接受以下类型的输入:

111.11,
11.11,
1.11,
1.01,
100,
10,
1,

正如在注释中提到的,空格是在正则表达式模式中按字面解释的字符

因此,在正则表达式的这一部分:

([.][0-9]{1,3})

  • [0-9]
    之间应有一个空格
  • 这同样适用于
    [0-9]
    之后,正则表达式将
    1
    匹配到
    3
    空格
这就是说,为了可读性,您有几种方法来构造正则表达式

1)将注释从正则表达式中删除:

string myregex = @"\s" // Match any whitespace once
+ @"\n"  // Match one newline character
+ @"[a-zA-Z]";  // Match any letter
2)使用语法在正则表达式中添加注释
(?#注释)

3)在正则表达式中激活自由间距模式:

string myregex = @"\s" // Match any whitespace once
+ @"\n"  // Match one newline character
+ @"[a-zA-Z]";  // Match any letter
文件:


看起来您所要做的就是从正则表达式模式中删除空格。为什么您首先要添加它们?如果我们已经回答了您的问题,请您接受以下详细说明的回答/投票:
nee # this will find a nee...
dle # ...dle (the split means nothing when white-space is ignored)