Regular expression to validate a string in a specific format

Can someone please help me put together a regular expression to validate an alphanumeric string in a specific format.

The first character must be a letter, and the next 6 characters are numbers ... for example, x279833 or X279833 are valid.

This is what I came up with - ^ [A-Za-z] {1} [0-9] {6} $

considers

+3
source share
3 answers

Yours should work fine (you edited it after I wrote this answer), but {1}absolutely not needed. You can shorten it a bit to use \dinstead of [0-9].

If you want the entire string to be in this format, use:

^[a-zA-Z]\d{6}$
+5
source

- :

^[a-zA-Z]\d{6}$
  • [a-zA-Z] -
  • \d char
  • {6} 6 , 6
+4

, , , , . , . - :

^[[:alpha:]]\d{6}$
+2

Source: https://habr.com/ru/post/1754908/


All Articles