Gav*_*lyn 2 c# regex pattern-matching do-while
我正在尝试编写一些非常基本的代码,但我也在正则表达式上挑战自己.我已经能够将代码混淆到一定程度,但我真正遇到问题的是我正在尝试运行do ... while循环而表达式为false.在这个时间点我绝对没有错误,但是do ... while循环继续运行.
我在下面附上相关代码,这里希望它有所帮助.
先感谢您
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
注意:删除了1行重复代码,更新后使用我推荐的正则表达式并删除Math.Round
[\d .]+
Run Code Online (Sandbox Code Playgroud)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$
Run Code Online (Sandbox Code Playgroud)

解释:
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) «$»
Run Code Online (Sandbox Code Playgroud)
将匹配:
1,432.01
456.56
654,246.43
432
321,543
14239729
21379312.32
Run Code Online (Sandbox Code Playgroud)
不会匹配
324,123.432
,,,312,.32
123,.23
Run Code Online (Sandbox Code Playgroud)
取自我的答案php - regex - 如何从字符串中提取带小数(点和逗号)的数字(例如1,120.01)?
| 归档时间: |
|
| 查看次数: |
85 次 |
| 最近记录: |