0

I need to create a regex to test below kind of data,

xxx_yyy_zzz-aaa

I am able to verify the first two _ underscore, but unable to append the - hyphens.

@"[a-zA-Z0-9]_[a-zA-Z0-9]_[a-zA-Z0-9]s/[^-][a-zA-Z0-9]"

I am using c#. the number of characters above are just for an example

Pawan Dubey
  • 192
  • 3
  • 15

1 Answers1

2

The xxx_yyy_zzz-aaa string implies that the format is {alphanum}_{alphanum}_{alphanum}-{alphanum}. The pattern for the {alphanum} part has already been written by you.

Next, you want to quantify each alphanumeric part since just [A-Za-z0-9] matches a single alphanum char. Use + to match 1 or more occurrences, or {3} to match only 3, or {3,} to match 3 or more.

That is not all, since you expect the whole string to match the pattern. Hence, you need anchors, ^ to match the start of string and $ (or \z) to match the end of string.

Thus, I'd recommend

@"^[a-zA-Z0-9]+_[a-zA-Z0-9]+_[a-zA-Z0-9]+-[a-zA-Z0-9]+\z"

See the regex demo.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563