using System;
using System.Text .RegularExpressions ;
public class Test
{
public static Decimal ExtractDecimalFromString( string str)
{
Regex digits = new Regex( @"^\D*?((-?(\d+(\.\d+)?))|(-?\.\d+)).*" ) ;
Match mx = digits.Match ( str) ;
//Console.WriteLine("Input {0} - Digits {1} {2}", str, mx.Success, mx.Groups);
return mx.Success ? Convert.ToDecimal ( mx.Groups [ 1 ] .Value ) : 0 ;
}
public static void ProcessString( string str) {
Console.WriteLine ( "{0} -> {1}" , str, ExtractDecimalFromString( str) ) ;
}
public static void Main( )
{
Console.WriteLine ( "Conversions" ) ;
ProcessString( "12345" ) ;
ProcessString( "12345.678" ) ;
ProcessString( "I have $.678 to my name" ) ;
ProcessString( "I have $-.678 to my name" ) ;
ProcessString( "-12345.678" ) ;
ProcessString( "-12345.678 987" ) ;
ProcessString( " b987z " ) ;
ProcessString( "Bill: -12345.678 OK" ) ;
ProcessString( "Hello World" ) ;
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uVGV4dC5SZWd1bGFyRXhwcmVzc2lvbnM7CgpwdWJsaWMgY2xhc3MgVGVzdAp7CiAgICAgICAgcHVibGljIHN0YXRpYyBEZWNpbWFsIEV4dHJhY3REZWNpbWFsRnJvbVN0cmluZyhzdHJpbmcgc3RyKQogICAgICAgIHsKCgkgICAgICAgIFJlZ2V4IGRpZ2l0cyA9IG5ldyBSZWdleChAIl5cRCo/KCgtPyhcZCsoXC5cZCspPykpfCgtP1wuXGQrKSkuKiIpOwogICAgICAgIAlNYXRjaCBteCA9IGRpZ2l0cy5NYXRjaChzdHIpOwogICAgICAgIAkvL0NvbnNvbGUuV3JpdGVMaW5lKCJJbnB1dCB7MH0gLSBEaWdpdHMgezF9IHsyfSIsIHN0ciwgbXguU3VjY2VzcywgbXguR3JvdXBzKTsKICAgICAgICAgICAgCiAgICAgICAgICAgIHJldHVybiBteC5TdWNjZXNzID8gQ29udmVydC5Ub0RlY2ltYWwobXguR3JvdXBzWzFdLlZhbHVlKSA6IDA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