2

I was trying to find solution, but couldn't. What will be regex when I have a string of defined length (let's say 10), and one of characters have to be specific letter (let's say 'd').

My first idea was something like this:

^([0-9d]{10})$

But it allows multiple 'd' in string, and I want exactly one.

0

1 Answer 1

4

You may use

^(?=.{10}$)[0-9]*d[0-9]*$

See the regex demo

Details

  • ^ - start of string
  • (?=.{10}$) - any 10 chars (other than a newline) followed with the end of string (you may precise this part if you wish, (?=[0-9d]{10}$))
  • [0-9]* - 0+ digits
  • d - a d
  • [0-9]* - 0+ digits
  • $ - end of string.
Sign up to request clarification or add additional context in comments.

1 Comment

wow, that's pretty clever. I was trying to use negative look aheads for not d

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.