问题
I'm trying to write some very basic code, but I'm also challenging myself on regular expression. I've been able to muddle through the code up to a point, but where I'm really having a problem is that I'm trying to run a do...while loop while the expression is false. At this point in time I get absolutely no errors, but the do...while loop keeps running.
I'm attaching the relevant code below, here's hoping it helps.
Thank you in advance
if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
string tollTax = Console.ReadLine();
Match toll = Regex.Match (tollTax, @"[\d .]+");
if (toll.Success)
{
Math.Round(Convert.ToDecimal(tollTax), 2);
Console.WriteLine("Good lord that's high... well it's your money");
}
else
{
do
{
Console.WriteLine("Please enter a proper number");
tollTax = Console.ReadLine();
}
while (toll.Success == false);
}
}
回答1:
Simple coding bug...comments added to your code to explain issue
if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
string tollTax = Console.ReadLine();
//toll.Success gets set here.
Match toll = Regex.Match(tollTax, @"[\d .]+");
if (toll.Success)
{
//Not sure why you are doing this since you aren't using it in the given code
Math.Round(Convert.ToDecimal(tollTax), 2);
Console.WriteLine("Good lord that's high... well it's your money");
}
else
{
//This is an infinite loop because toll.Success is never set again.
do
{
Console.WriteLine("Please enter a proper number");
tollTax = Console.ReadLine();
} while (toll.Success == false);
}
}
What i think you want
if (tollResponse == "yes")
{
Console.WriteLine("How much do you pay per trip?");
//Loop over the Console.ReadLine() using the else statement and exit if it is right the first time
do
{
string tollTax = Console.ReadLine();
//toll.Success gets set here.
Match toll = Regex.Match(tollTax, @"^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$");
if (toll.Success)
{
Console.WriteLine("Good lord that's high... well it's your money");
//will exit
}
else
{
Console.WriteLine("Please enter a proper number");
}
} while (toll.Success == false);
}
Note: Removed 1 line of duplicate code as well, updated to use my recommended regex and removed Math.Round
Regex Tool
[\d .]+
Debuggex Demo
A more Valid Regex for currency
decimal optional (two decimal places)
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$
Debuggex Demo
Explained:
number (decimal required)
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$
Options: case insensitive
Assert position at the beginning of the string «^»
Match a single character present in the list below «[+-]?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
The character “+” «+»
The character “-” «-»
Match a single character in the range between “0” and “9” «[0-9]{1,3}»
Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3}»
Match the regular expression below «(?:,?[0-9]{3})*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “,” literally «,?»
Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
Match a single character in the range between “0” and “9” «[0-9]{3}»
Exactly 3 times «{3}»
Match the character “.” literally «\.»
Match a single character in the range between “0” and “9” «[0-9]{2}»
Exactly 2 times «{2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»
Will Match:
1,432.01
456.56
654,246.43
432
321,543
14239729
21379312.32
Will not Match
324,123.432
,,,312,.32
123,.23
taken from my answer here php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?
回答2:
The problem is that your toll
variable is not updated in your loop.
You would have to add this to your do
block to make it work:
toll = Regex.Match (tollTax, @"[\d .]+");
来源:https://stackoverflow.com/questions/24690630/setting-a-relevant-expression-match-to-false-in-a-do-while