Need help with regex

I need a regular expression for which:

the string is alphanumeric and has exactly 6 characters in the first half, followed by a hyphen (optional), followed by optional 4 characters: (in the second half, no more than 4 characters)

so any of the following is valid:

11111A
111111-1
111111-yy
yyyyy-989
yyyyyy-9090

I thought this expression /[a-zA-Z0-9]([-])?[a-zA-Z0-9]{5,10}$/;should work, but I can't get it to work correctly.

Any help would be appreciated

+3
source share
4 answers
^ [a-zA-Z0-9] {6} (- [A-Za-z0-9] {1,4})? $
+5
source
^[a-zA-Z0-9]{6}-?[a-zA-Z0-9]{0,4}$

, , . Tanascius Streetpc ______-____, \w .

: , , , , , 111111AAAA ? , , Tanascius .

, :

^[a-zA-Z0-9]{6}(?:-[a-zA-Z0-9]{1,4})?$

backreferenceless .

. 111111-, 11111-A. 111111-, 111111A, :

^[a-zA-Z0-9]{6}(?:-[a-zA-Z0-9]{0,4})?$

EDIT: Tanascius \w, .

+1

6 4 -:

^\w{6}-?\w{0,4}$

: \w - [a-zA-Z0-9_], (_). , [a-zA-Z0-9].

UPDATE

:

  • , , () 0
    ^\w{5}[a-zA-Z1-9]-?\w{0,3}[a-zA-Z1-9]$
  • , , "" ():
    ^(?!0{6})\w{6}-?(?!0+$)\w{0,4}$
    " " , , , , ( ). , , 0. , (+) 0s ($) /.
0

, , , txt2regex, . http://txt2regex.sourceforge.net.

I cannot help but quote this: "The programmer had a problem. He realized that he could solve it using a regular expression. Now he has two problems."

0
source

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


All Articles