Please help me make up a regular schedule for such conditions: the text should not consist only of numbers, the text can consist of numbers, Latin letters of both registers, underscores and a hyphen _- .

PS However, I can’t even give an example. I know how to find the digits \d , I know how to find the letters and symbols [A-Za-z-_] , but I don’t know how to make a string just be digital. :(

PPS It may be clearer if I indicate that I use the regulars in django-vskom RegexValidator to validate the model field.

The initial can be any of the characters, including the number. It does not matter. The main thing that the line could not consist only of numbers.

  • Specify the rule for the initial characters, and then for all other [condition1] [condition2] + Something like this: [A-Za-z_][A-Za-z0-9_]+ - nick_n_a
  • Initial characters? In the sense of? The initial can be any of the characters, including the number. It does not matter. The main thing that the line could not consist only of numbers. - Yalierar
  • In most cases, an identifier is a value that does not begin with a digit, you have another case — then the condition is different. - nick_n_a
  • Alas. Such a task. My decision is not made when the first character cannot be a number. - Yalierar

2 answers 2

Try this:

 ^(?!\d+$)[\da-zA-Z_-]+$ 
  • Hmm, it seems to work too. A regular is easier than the one below. : D - Yalierar
  • In the regular schedule above, positional checks are not used, therefore it is more difficult - ReinRaus
  • Let me simplify a little: ^(?!\d+$)[[:alnum:]_-]+$ - ValeriyKr
  • alnum? What is it? :) Most likely it does not work everywhere, for example, I test here - it does not plow any more, but it works here . - Yalierar
  • This is a defined POSIX character class, which means exactly what I replaced. Usually, if there is support for advanced / retrospective checks, then they are even more so. Plus (subjectively) is easier to read. - ValeriyKr

Your condition is satisfied by the following regular expression:

 /^[\da-z_-]*([a-z_-]+\d*|\d*[a-z_-]+)[\da-z_-]*$/i 

[a-z_-]+\d*|\d*[a-z_-]+ - this part makes it necessary before or after the digit to meet a non-numeric character from the allowed set.

View an example of work: https://regex101.com/r/ZyQw18/1


Update: simplified regular expression

 /^([a-z_-]+\d*|\d*[a-z_-]+)+$/i 

View an example of work: https://regex101.com/r/ZyQw18/4


Update 2: the solution is even faster

 /^[\da-z_-]*[a-z_-][\da-z_-]*$/i 

View an example of work: https://regex101.com/r/ZyQw18/2

  • It seems to work, thanks! - Yalierar
  • Your regular expression can be simplified, because the problem can be reformulated as follows: “the string must contain at least one NOT numeric literal”. And if you just look at a regular expression, then in the first alternative, \d* appears in [\da-z_-]*$ , in the second alternative, it is similar. - ReinRaus
  • @ReinRaus, I simplified the regular expression - VenZell
  • Can be a little easier: regex101.com/r/ZyQw18/2 - ReinRaus
  • @ReinRaus, thanks, added to the response with a link to you - VenZell