用于验证货币字符串C#的正则表达式查询?

用于验证货币字符串C#的正则表达式查询?,c#,regex,validation,currency-formatting,C#,Regex,Validation,Currency Formatting,我决不是Regex的大师,这就是为什么我在这里,我现在有: \s?[^0-9]?\s?[0-9,]+([\\.]{1})?[0-9]+\s? 到regex101 为了解释我的验证尝试,我尝试验证字符串是否与正确的格式结构匹配 我只想匹配字符串,例如: 1.00英镑 10.00英镑 100英镑 1000.00英镑 10000.00英镑 100000.00英镑 1234546.00英镑 验证规则: 字符串的开头必须包含一个“£” 字符串的小数点后应始终有2位数字 在“£”之后,只接受0-9之

我决不是
Regex
的大师,这就是为什么我在这里,我现在有:

\s?[^0-9]?\s?[0-9,]+([\\.]{1})?[0-9]+\s?
到regex101

为了解释我的验证尝试,我尝试验证字符串是否与正确的格式结构匹配

我只想匹配字符串,例如:

  • 1.00英镑
  • 10.00英镑
  • 100英镑
  • 1000.00英镑
  • 10000.00英镑
  • 100000.00英镑
  • 1234546.00英镑
验证规则

  • 字符串的开头必须包含一个“£”
  • 字符串的小数点后应始终有2位数字
  • 在“£”之后,只接受0-9之间的数字
  • 如果字符串长度大于6(1000.00英镑之后),则需要在适当的位置输入逗号(即10000.00英镑-100000.00英镑-1000000.00英镑-10000000.00英镑等)
例如,不应接受的字符串:

  • 1英镑
  • 10000英镑
  • 1,00.00英镑
  • 1000.00英镑
  • 10000.00
  • 100000,00英镑
  • 1234.546英镑
真的希望你们中的一个了不起的人能帮助我,如果你需要更多的信息,请让我知道

这个怎么样

new Regex(@"£\d{1,3}(\,\d{3})*\.\d{2}\b");
编辑:

这个怎么样

new Regex(@"£\d{1,3}(\,\d{3})*\.\d{2}\b");
编辑:

您可以尝试以下方法:

当便士是强制性的-
\[0-9]{2}
我们有3英镑选项:

  [0-9]{1,4}                   for sums in range £0 .. £9999
  [0-9]{2,3},[0-9]{3}          for sums in range £10,000 .. £999,999
  [0-9]{1,3}(?:,[0-9]{3}){2,}  for huge sums     £1,000,000 ..
演示:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  Regex regex = new Regex(
    @"^£(?:[0-9]{1,4}|[0-9]{2,3},[0-9]{3}|[0-9]{1,3}(?:,[0-9]{3}){2,})\.[0-9]{2}$";

  string[] tests = new string[] {
    "£1.00",
    "£10.00",
    "£100.00",
    "£1000.00",
    "£10,000.00",
    "£100,000.00",
    "£1,234,546.00",

    "£1",
    "£10.000",
    "£1,00.00",
    "£1,000.00",
    "10,000.00",
    "£100,000,00",
    "£1.234.546",
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,15} : {(regex.IsMatch(test) ? "Match" : "Fail")}"));

  Console.Write(report);
          £1.00 : Match
         £10.00 : Match
        £100.00 : Match
       £1000.00 : Match
     £10,000.00 : Match
    £100,000.00 : Match
  £1,234,546.00 : Match
             £1 : Fail
        £10.000 : Fail
       £1,00.00 : Fail
      £1,000.00 : Fail
      10,000.00 : Fail
    £100,000,00 : Fail
     £1.234.546 : Fail
结果:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  Regex regex = new Regex(
    @"^£(?:[0-9]{1,4}|[0-9]{2,3},[0-9]{3}|[0-9]{1,3}(?:,[0-9]{3}){2,})\.[0-9]{2}$";

  string[] tests = new string[] {
    "£1.00",
    "£10.00",
    "£100.00",
    "£1000.00",
    "£10,000.00",
    "£100,000.00",
    "£1,234,546.00",

    "£1",
    "£10.000",
    "£1,00.00",
    "£1,000.00",
    "10,000.00",
    "£100,000,00",
    "£1.234.546",
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,15} : {(regex.IsMatch(test) ? "Match" : "Fail")}"));

  Console.Write(report);
          £1.00 : Match
         £10.00 : Match
        £100.00 : Match
       £1000.00 : Match
     £10,000.00 : Match
    £100,000.00 : Match
  £1,234,546.00 : Match
             £1 : Fail
        £10.000 : Fail
       £1,00.00 : Fail
      £1,000.00 : Fail
      10,000.00 : Fail
    £100,000,00 : Fail
     £1.234.546 : Fail
您可以尝试以下方法:

当便士是强制性的-
\[0-9]{2}
我们有3英镑选项:

  [0-9]{1,4}                   for sums in range £0 .. £9999
  [0-9]{2,3},[0-9]{3}          for sums in range £10,000 .. £999,999
  [0-9]{1,3}(?:,[0-9]{3}){2,}  for huge sums     £1,000,000 ..
演示:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  Regex regex = new Regex(
    @"^£(?:[0-9]{1,4}|[0-9]{2,3},[0-9]{3}|[0-9]{1,3}(?:,[0-9]{3}){2,})\.[0-9]{2}$";

  string[] tests = new string[] {
    "£1.00",
    "£10.00",
    "£100.00",
    "£1000.00",
    "£10,000.00",
    "£100,000.00",
    "£1,234,546.00",

    "£1",
    "£10.000",
    "£1,00.00",
    "£1,000.00",
    "10,000.00",
    "£100,000,00",
    "£1.234.546",
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,15} : {(regex.IsMatch(test) ? "Match" : "Fail")}"));

  Console.Write(report);
          £1.00 : Match
         £10.00 : Match
        £100.00 : Match
       £1000.00 : Match
     £10,000.00 : Match
    £100,000.00 : Match
  £1,234,546.00 : Match
             £1 : Fail
        £10.000 : Fail
       £1,00.00 : Fail
      £1,000.00 : Fail
      10,000.00 : Fail
    £100,000,00 : Fail
     £1.234.546 : Fail
结果:

  using System.Linq;
  using System.Text.RegularExpressions;

  ...

  Regex regex = new Regex(
    @"^£(?:[0-9]{1,4}|[0-9]{2,3},[0-9]{3}|[0-9]{1,3}(?:,[0-9]{3}){2,})\.[0-9]{2}$";

  string[] tests = new string[] {
    "£1.00",
    "£10.00",
    "£100.00",
    "£1000.00",
    "£10,000.00",
    "£100,000.00",
    "£1,234,546.00",

    "£1",
    "£10.000",
    "£1,00.00",
    "£1,000.00",
    "10,000.00",
    "£100,000,00",
    "£1.234.546",
  };

  string report = string.Join(Environment.NewLine, tests
    .Select(test => $"{test,15} : {(regex.IsMatch(test) ? "Match" : "Fail")}"));

  Console.Write(report);
          £1.00 : Match
         £10.00 : Match
        £100.00 : Match
       £1000.00 : Match
     £10,000.00 : Match
    £100,000.00 : Match
  £1,234,546.00 : Match
             £1 : Fail
        £10.000 : Fail
       £1,00.00 : Fail
      £1,000.00 : Fail
      10,000.00 : Fail
    £100,000,00 : Fail
     £1.234.546 : Fail

这些特定的验证规则有什么原因吗?看起来你可以用它来代替正则表达式。如果你这样做,你说应该失败的大多数例子都会成功,但我不确定为什么会有问题。例如,为什么
.1
会失败?那不应该是1英镑吗?而
.10.000
不应该只是
.10.00
吗?验证规则是与工作相关的。如果测试数据显示一个
.1
字符串作为基金价值,那么它表明格式有问题,需要在自动化测试中反映出来。这些特定的验证规则有什么原因吗?看起来你可以用它来代替正则表达式。如果你这样做,你说应该失败的大多数例子都会成功,但我不确定为什么会有问题。例如,为什么
.1
会失败?那不应该是1英镑吗?验证规则是与工作相关的。如果测试数据显示一个
.1
字符串作为基金价值,那么它表明格式有问题,需要在自动测试中反映出来。这与我需要的非常接近。仅匹配错误的1000.00英镑。它应该匹配1000.00英镑,而不是1000.00英镑。你能帮我把它改成和那个相匹配的吗@亚历克斯:这太接近我需要的了。仅匹配错误的1000.00英镑。它应该匹配1000.00英镑,而不是1000.00英镑。你能帮我把它改成和那个相匹配的吗@亚历克斯先生,你是上帝!你是否碰巧知道一个好地方,我可以在Regex上阅读,我希望能够正确使用它,但sytax对我来说是陌生的?@HermanCodes:有很多Regex教程,例如:非常感谢你,如果可以的话,我会投你一票:先生,你是上帝!你是否碰巧知道一个好地方,我可以阅读Regex?我很想能够正确使用它,但sytax对我来说是陌生的?@HermanCodes:有很多Regex教程,例如:非常感谢你,如果可以的话,我会投你一票:D