简体   繁体   English

正则表达式C#

[英]Regular Expressions C#

I know there are many questions about making regular expressions, but they all seem to be about a single problem than the general usage. 我知道有很多关于制作正则表达式的问题,但它们似乎都只是一个问题而不是一般用法。 I, too, have a problem like to solve. 我也有一个问题要解决。 I have tried to learn by reading about regular expressions, but it gets tricky quick. 我试着通过阅读正则表达式来学习,但它很快就变得棘手。 Here's my question: 这是我的问题:

C# C#

I need to validate two textboxes that exist on the same form. 我需要验证同一表单上存在的两个文本框。 The math operations I've coded can handle any floating point number. 我编码的数学运算可以处理任何浮点数。 For this particular application I know of three formats the numbers will be in or there is a mistake on the users behalf. 对于这个特定的应用程序,我知道数字将处于三种格式或代表用户有错误。 I'd like to prevent those mistakes in example if an extra number is accidentally typed or if enter is hit too early, etc. 如果一个额外的号码被意外输入或者输入太早等,我想在例子中防止这些错误。

Here are the formats: "#.####" "##.####" "###.##" where the "#" represents a mandatory digit. 以下是格式:“#。####”“##。####”“###。##”其中“#”表示必填数字。 The formats starting with a one or two digit whole number must have 4 trailing digits or more. 以一位或两位数整数开头的格式必须有4个或更多的尾随数字。 I've capped it at 8, or so I tried to lol.The format starting with a three digit whole number should never be allowed to have more than two digits trailing the decimal. 我把它限制在8,或者我试图大声笑。从三位数整数开始的格式永远不应该允许有两位数以上的小数。

Here's what I have tried thus far. 这是我到目前为止所尝试的内容。

    Regex acceptedInputRegex = new Regex(@"^\b[0-9]{3}.[0-9]{2}|[0-9]{1,2}.[0-9]{4,8}$");

    Regex acceptedInputRegex = new Regex(@"^\b\d{3}.\d{2} | \d{1,2}.\d{4,8}$");

I have tried it in thinking a match was what I wanted to achieve and as if a match to my negated expression means there is a problem. 我已经尝试过,我认为匹配是我想要实现的,并且好像与我的否定表达相匹配意味着存在问题。 I was unsuccessful in both attempts. 我在两次尝试都没有成功。 This is the code: 这是代码:

    if (acceptedInputRegex.IsMatch(txtMyTextBox1.Text) || acceptedInputRegex.IsMatch(txtMyTextBox2.Text))
            {

            } else
            {
                MessageBox.Show("Numbers are not in the right format", "Invalid Input!");
                return;
            }
  1. Are regular expressions what I should be using to solve this problem? 正则表达式是我应该用来解决这个问题的吗?
  2. If not, please tell me what you recommend. 如果没有,请告诉我你的建议。 If so, please help me correct my regex. 如果是这样,请帮我纠正我的正则表达式。

Thanks. 谢谢。

You are close, you need to escape the dots and group the alternatives so that the ^ and $ anchors could be applied to both of them: 你很接近,你需要逃避点和组合替代,以便^$锚可以应用于它们:

@"^(?:\d{3}\.\d{2}|\d{1,2}\.\d{4,8})$"

See the regex demo . 请参阅正则表达式演示

Details : 细节

  • ^ - start of string ^ - 字符串的开头
  • (?: - start of a non-capturing group matching either of the two alternatives: (?: - 与两个备选方案中的任何一个匹配的非捕获组的开始:
    • \\d{3}\\.\\d{2} - 3 digits, . \\d{3}\\.\\d{2} - 3位数, . and 2 digits 和2位数
    • | - or - 要么
    • \\d{1,2}\\.\\d{4,8} - 1 or 2 digits, . \\d{1,2}\\.\\d{4,8} - 1或2位数, . , 4 to 8 digits ,4到8位数
  • ) - end of the non-capturing group ) - 非捕获组的结束
  • $ - end of string. $ - 结束字符串。

To make \\d match only ASCII digits, use RegexOptions.ECMAScript option: 要使\\d仅匹配ASCII数字,请使用RegexOptions.ECMAScript选项:

var isValid = Regex.IsMatch(s, @"^(?:\d{3}\.\d{2}|\d{1,2}\.\d{4,8})$", RegexOptions.ECMAScript);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM