0

I have validated my regex from regex storm My pattern is: ([A-Z])([A-Z])([A-Z])-([0-9])([0-9])([0-9]) which simply validates a string with patter XXX-123

I have the following program:

Regex regex = new Regex(@"([A - Z])([A - Z])([A - Z])-([0 - 9])([0 - 9])([0 - 9])");
    private void planeIdentityTB_TextChanged(object sender, EventArgs e)
    {
        Match match = regex.Match(planeIdentityTB.Text);
        if (!match.Success)
        {
            isPlaneIdValid = false;
            planeIdentityTB.BackColor = Color.Red;
        } else
        {
            isPlaneIdValid = true;
            planeIdentityTB.BackColor = Color.White;
        }

    }

The match is never successfull. I think i am making a silly mistake. But i am not being able to see it. Can anyone help me?

Skaranjit
  • 588
  • 4
  • 24
  • 4
    Are you sure the spaces are there in your real code? Is it a copy/paste error? – Wiktor Stribiżew Oct 07 '18 at 09:01
  • 5
    But you added spaces between `A` and `-`, meaning you now accept `A`, the space, the dash `-` and `Z`. – Willem Van Onsem Oct 07 '18 at 09:01
  • The above duplicate is however "rather broad". It simply is a reference to regular expressions, but does not really answers the problem that occurs here. – Willem Van Onsem Oct 07 '18 at 09:05
  • @WiktorStribiżew yes it was a copy/paste error. Thank you! I didn't see that :D :D – Skaranjit Oct 07 '18 at 09:12
  • @WillemVanOnsem First and foremost, there is no problem here, just a typo. And it is a valid dupe since there is all the details for OP to learn the basics: space matches a space, `-` in a char class defines a range. – Wiktor Stribiżew Oct 07 '18 at 09:14
  • @WiktorStribiżew: where does the linked answer says space does matter? – Willem Van Onsem Oct 07 '18 at 09:15
  • @WillemVanOnsem There are enough details about character classes. Besides, it is a **typo** question, no need answering it. – Wiktor Stribiżew Oct 07 '18 at 09:27
  • @WiktorStribiżew: the origin of a problem is irrelevant. The question is whether a person can make this mistake without a typo. That is possible, especially since some regex parsers allow spaces in a character group. Furthermore if we take the typo argument, then this question is not a dupe, but should be closed as a typo-question. The first argument itself is problematic as well, since we could use the same argument to say that regular expressions themselves are just a a "link" to a finite state machine, and hence all regex questions boil down to "what is a finite state machine". – Willem Van Onsem Oct 07 '18 at 09:33
  • Take for example this question: https://stackoverflow.com/q/6017778/67579 This again simply boils down to some misunderstanding of regular expressions. But it is more specific, since the OP does not seem to understand what character groups are. Perhaps this was a typo as well, but still it is useful to understand that a character group needs to be surrounded with square brackets. Sure the above question is a dupe, but not really a "pars-pro-toto" dupe. – Willem Van Onsem Oct 07 '18 at 09:37

0 Answers0