using System;
using System.Text.RegularExpressions;
public class Test
{
public static void Main ()
{
// Define a regular expression for currency values.
Regex rx = new Regex(@"^-?/d+(/./d{2})?$");
// Define some test strings.
string[] tests = {"-42", "19.99", "0.001", "100 USD"};
// Check each test string against the regular expression.
foreach (string test in tests)
{
if (rx.IsMatch(test))
{
Console.WriteLine("{0} is a currency value.", test);
}
else
{
Console.WriteLine("{0} is not a currency value.", test);
}
}
}
}
本文通过C#编程语言使用正则表达式来验证字符串是否符合货币数值的格式。演示了如何定义匹配货币格式的正则表达式,并对几个示例字符串进行匹配测试。
1682

被折叠的 条评论
为什么被折叠?



