Having a hard time with Regex.
I have this string:
I need to replace this whole number "11.000000" with another number.
How do I identify this string by saying:
Give me the string right before "%" until you reach the first blank space (" ")?
Try this:
string pattern = @"^Fixed Breakeven with (\d+(\.\d+)?)% Fees$";
string input = "Fixed Breakeven with 11.0000000% Fees";
var match = Regex.Match(input, pattern);
string output = string.Empty;
if (match != null)
{
output = match.Groups[1].Value;
}
You can use LINQ:
var myString = "Fixed Breakeven with 11.0000000% Fees";
var number = myString.Split('%').First().Split(' ').Last();
You can try this also, using regex. This is generic method to fetch decimal from string. It will work for all cases.
public static bool ExtractDecimalFromString(string inputString, out Decimal decimalValue)
{
Regex regex = new Regex(@"^\D*?((-?(\d+(\.\d+)?))|(-?\.\d+)).*");
Match match = regex.Match(inputString);
decimalValue = match.Success ? Convert.ToDecimal(match.Groups[1].Value) : 0;
return match.Success;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.