Reza
Reza

Reputation: 73

How to validate Regex

Im having a hard time with grouping parts of a Regex. I want to validate a few things in a string that follows this format: I-XXXXXX.XX.XX.XX

  1. Validate that the first set of 6 X's (I-xxxxxx.XX.XX.XX) does not contain characters and its length is no more than 6.

  2. Validate that the third set of X's (I-XXXXXX.XX.xx.XX) does not contain characters and is only 1 or 2.

Now, I have already validation on the last set of XX's to make sure the numbers are 1-8 using

string pattern1 = @"^.+\.(0?[1-8])$";

Match match = Regex.Match(TxtWBS.Text, pattern1);
            if (match.Success)
            ;
            else
            { errMessage += "WBS invalid"; errMessage += 
Environment.NewLine; }

I just cant figure out how to target specific parts of the string. Any help would be greatly appreciated and thank you in advance!

Upvotes: 0

Views: 107

Answers (3)

Genusatplay
Genusatplay

Reputation: 771

^I-([0-9]{1,6})\.(.{1,2})\.(0[1-2])\.(.{1,2})$

groups delimited by . (\.) :

  1. ([0-9]{1,6}) - 1-6 digits
  2. (.{1,2}) - 1-2 any single character
  3. (0[1-2]) - 01 or 02
  4. (.{1,2}) - 1-2 any single character

you can write and easy test regex on your input data, just google "regex online"

Upvotes: 0

This regex:

^.-[0-9]{6}(\.[1-8]{1,2}){3}$

will validate the following:

  1. The first character can be any character, but is of length 1
  2. It is followed by a dash
  3. The dash is followed by exactly 6 numbers 0 - 9. (If this could be less than 6 characters - for example, between 3 and 6 characters - just replace {6} with {3,6}).
  4. This is followed by 3 groups of characters. Each of this groups are proceeded by a period, are of length 1 or 2, and can be any number 1 - 8.

An example of a valid string is:

I-587954.12.34.56

This is also valid:

I-587954.1.3.5

But this isn't:

I-587954.12.80.356

because the second-to-last group contains a 0, and because the last group is of length 3.

Pleas let me know if I have misunderstood any of the rules.

Upvotes: 0

Elizabeth
Elizabeth

Reputation: 499

You're having some trouble adding new validation to this string because it's very generic. Let's take a look at what you're doing:

^.+\.(0?[1-8])$

This finds the following:

  • ^ the start of the string

  • .+ everything it can, other than a newline, basically jumping the engine's cursor to the end of your line

  • \. the last period in the string, because of the greedy quantifier in the .+ that comes before it

  • 0? a zero, if it can

  • [1-8] a number between 1 and 8

  • ()$ stores the two previous things in a group, and if the end of the string doesn't come after this, it may even backtrace and try the same thing from the second to last period instead, which we know isn't a great strategy.

This ends up matching a lot of weird stuff, like for example the string The number 0.1

Let's try patterning something more specific, if we can:

^I-(\d{6})\.(\d{2})\.(\d{1,2})\.([1-8]{2})$

This will match:

  • ^I- an I and a hyphen at the start of the string

  • (\d{6}) six digits, which it stores in a capture group

  • \. a period. By now, if there was any other number of digits than six, the match fails instead of trying to backtrace all over the place.

  • (\d{2})\. Same thing, but two digits instead of six.

  • (\d{1,2})\. Same thing, the comma here meaning it can match between one and two digits.

  • ([1-8]{2}) Two digits that are each between 1 and 8.

  • $ The end of the string.

I hope I understood what exactly you're trying to match here. Let me know if this isn't what you had in mind.

Upvotes: 1

Related Questions