-2

The front part is letters [a-zA-Z] (necessary), and the back part is digits (optional). total length >=1 && <= 80.

Should match the following:

a
a1
ab12
aAc
aAd12

Should not match following:

1
1a
juzraai
  • 4,835
  • 8
  • 26
  • 41
Hel
  • 305
  • 3
  • 11

2 Answers2

4

In order to limit the whole length to 80:

^(?i)(?=.{1,80}$)[a-z]+\d*$

Explanation:

^               : beginning of line
  (?i)          : case insensitive
  (?=.{1,80}$)  : positive lookahead, make sure we have 1 upto 80 characters
  [a-z]+        : 1 or more letters
  \d*           : 0 or more digits
$               : end of line
Toto
  • 83,193
  • 59
  • 77
  • 109
1

You could use anchors to assert the start ^ and the end $ of the line.

To match a lower or uppercase character one or more times you could use [a-zA-Z]+ followed by matching a digit 0 - 80 using a quantifier \d{0,80}

^[a-zA-Z]+\d{0,80}$

Edit:

If the total length should be 1 -80 you could use a positive lookahead (?= to assert that what follows is [a-zA-Z0-9]{1,80}until the end of the line $

Then match one or more times a lower or uppercase character [a-zA-Z]+ followed by zero or more times a digit [0-9]*.

^(?=[a-zA-Z0-9]{1,80}$)[a-zA-Z]+[0-9]*$

The fourth bird
  • 96,715
  • 14
  • 35
  • 52
  • 3
    Wasn't it the total length that was supposed to be 1 - 80 not only the digit part? – SHN Jul 15 '18 at 09:37
  • 1
    @S.H.N In that case the OP could use a positive lookahead [`^(?=[a-zA-Z0-9]{1,80}$)[a-zA-Z]+[0-9]*$`](https://regex101.com/r/u0DkJI/1) – The fourth bird Jul 15 '18 at 09:46
  • 1
    Yeah. It should be total length. It's appreciate if you can update in the answer. – Hel Jul 15 '18 at 09:57