2

Please help me correct this regular expression in C# to match/validate only when the following is true:

  • Always starts with da/
  • At least one single character after da/
  • Only non capitals are allowed, range from a-z (both included)
  • digits 0-9 allowed
  • dashes are allowed (-)

This is what I have, but it's not working:

/^da/+[a-z0-9+-]+$/

Example of accepted string that will validate the regular expression:

da/this-will-validate-correct-1
1
  • 1
    but not working - how exactly doesn't it work? Please provide sample input and expected output. Most probably you just need ^da/[a-z0-9+-]+$ Commented May 15, 2016 at 15:26

2 Answers 2

2

Your regex allows 1 or more / after da and the + inside the character class allowed + symbols.

Judging by the requirements, you just need

^da/[a-z0-9-]+$

See the regex demo

The + after the character class [a-z0-9+-] requires at least 1 character after da/.

Regex.IsMatch("da/this-will-validate-correct-1", @"^da/[a-z0-9-]+$")

See the C# demo

Pattern explanation:

  • ^ - start of string
  • da/ - a literal string of characters da/
  • [a-z0-9-]+ - 1 or more characters from a-z and 0-9 ranges or a -
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

Comments

0

you can try this ^da/[a-z0-9\-]+$

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.