Regex^ rx = gcnew Regex( "^-?\\d+(\\.\\d{2})?$" );
// Define some test strings.
array<String^>^tests = {"-42","19.99","0.001","100 USD"};
// Check each test string against the regular expression.
System::Collections::IEnumerator^ myEnum = tests->GetEnumerator();
while ( myEnum->MoveNext() )
{
String^ test = safe_cast<String^>(myEnum->Current);
if ( rx->IsMatch( test ) )
{
Console::WriteLine( "{0} is a currency value.", test );
}
else
{
Console::WriteLine( "{0} is not a currency value.", test );
}
}
查MSND的正则表达式时查到的代码,不明白 ^ 代表什么意思。请达人解释下
C#的版本应该是这样的:
Regex rx = new Regex( "^-?\\d+(\\.\\d{2})?$" );
// Define some test strings.
List<string> tests = {"-42","19.99","0.001","100 USD"};
// Check each test string against the regular expression.
System.Collections.IEnumerator myEnum = tests.GetEnumerator();
while ( myEnum.MoveNext() )
{
string test = (string)(myEnum.Current);
if ( rx.IsMatch( test ) )
{
Console.WriteLine( "{0} is a currency value.", test );
}
else
{
Console.WriteLine( "{0} is not a currency value.", test );
}
}
Regex rx = new Regex("^-?\\d+(\\.\\d{2})?$");// Define some test strings.
List<string> tests = new List<string> { "-42", "19.99", "0.001", "100 USD" };// Check each test string against the regular expression.
System.Collections.IEnumerator myEnum = tests.GetEnumerator();
while (myEnum.MoveNext())
{
string test = (string)(myEnum.Current);
if (rx.IsMatch(test))
{
Console.WriteLine("{0} is a currency value.", test);
}
else
{
Console.WriteLine("{0} is not a currency value.", test);
}
}
-? 负号,出现0次或1次
\d+ 数字,出现1次或多次
(
\. 小数点
\d{2} 数字,出现2次
)? 括号中的内容出现0次或1次
$ 字符串结尾
-? 负号,出现0次或1次
\d+ 数字,出现1次或多次
(
\. 小数点
\d{2} 数字,出现2次
)? 括号中的内容出现0次或1次
$ 字符串结尾 复制八楼 正则问题
$ 表示其前面的字符必须位于字符串的结束处
\b 匹配一个单词的边界
\B 匹配一个非单词的边界
\A 前面的字符必须位于字符处的开始处,\z 前面的字符必须位于字符串的结束处,\Z 前面的字符必须位于字符串的结束处,或者位于换行符前
看看正则表达式