1

I need a regex, that solves the follwing task: - input - a single char from a to j and a single number after the char from 1 to 10 (inclusive)

Correct: a1, a2, f3, b7, c10
Wrong: a11, b0, abv, a34, h11, 1c

I've tried these: ^[a-j]{1}[0-9|10]{1,2}$ ^[a-j]{1}[0-9]{2}$

Thanks in advance!

2
  • I've tried these: ^[a-j]{1}[0-9|10]{1,2}$; ^[a-j]{1}[0-9]{2}$ Commented Jun 20, 2015 at 14:39
  • What is the problem with those? Also, use the 'edit' link to update your question. Commented Jun 20, 2015 at 14:40

2 Answers 2

2

This should do it.

"^[a-j]([1-9]|10)$"

The first bracket matches the single letter, then there is an alternative which is either a single digit or the number 10.

^[a-j]{1}[0-9|10]{1,2}$

A {1} is redundant. [0-9|10] means: any of the characters 0, 1,... 9 or |, because [] acts like a set expression, with - for ranges. Also, you would permit this single character to be written once or twice, which would match a11, a12,...j99 etc.

^[a-j]{1}[0-9]{2}$

Better, but permitting any two digits after the letter.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh. I knew that I am missing something! Thanks a lot!
2

[..] is character class where you can specify single characters like [abc] will mean a or b or c. If you write it like [10] it will mean 1 or 0, not 10.

So instead of [0-9|10] which means 0 or 1 or .. or 9 or | or 1 or 0 you need to write something closer to [0-9]|10 (notice position of ]).
Also {1} is redundant.

Rest of your regex seems fine.

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.