1

I recently had test with regex, but I am not skilled in it. I try to write something for pattern:

  • String must contains 6-16 chars
  • String should be start only from char
  • String could contain any char, but "-" only once
  • String must not end with "-"

I wrote somethin like this "(^[a-zA-Z]).(/w{6,16}).*(?<!-)$", ofcourse it is not correct and not full.

I am very interesting in correct answear with explanaition, you could downvote my question if you think, it shoud be.

1
  • @WiktorStribiżew yaaaap seems correct. I will try to learn more about regex Commented Oct 10, 2018 at 20:06

1 Answer 1

1

You may use

^[a-zA-Z](?=.{5,15}$)[^-]*(?:-[^-]+)?$

See the regex demo

Details

  • ^ - start of string
  • [a-zA-Z] - an ASCII letter
  • (?=.{5,15}$) - a positive lookahead requiring 5 to 15 chars up to the string end from the current position
  • [^-]* - (a negated character class) 0+ chars other than -
  • (?:-[^-]+)? - an optional non-capturing group that matches 1 or 0 repetitions of
    • - - a hyphen
    • [^-]+ - 1+ chars other than -
  • $ - end of string
Sign up to request clarification or add additional context in comments.

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.