Regular expression with "not character" does not match expected

I am trying to fulfill the following restrictions:

  • Line
  • has 3 to 256 characters, which are az, 0-9, dashes, or periods.
  • this line cannot begin or end -

I want to get the following result:

aaa  -> good
aaaa -> good
-aaa -> bad
aaa- -> bad
---a -> bad

A have some regular expressions that do not give the correct answer: 1) ^[^-][a-z0-9\-.]{3,256}[^-]$gives all test lines as bad;

2) ^[^-]+[a-z0-9\-.]{3,256}[^-]+$treats the first three lines as one matching line, since [^ -] matches the new line that I am assuming.

3) ^[^-]?[a-z0-9\-.]{3,256}[^-]?$(? For one or zero matching dashes) gives all test lines as good

Where is the truth? I feel it is either close to mine, or much more complicated.

PS I am using python 3 re module.

+4
4

: ^[^-][a-z0-9\-.]{3,256}[^-]$

[^-] , {3,256} {1,254}

, , , a-z, 0-9 . ( , -), :

^[a-z0-9.][a-z0-9\-.]{1,254}[a-z0-9.]$
+5

lookahead, , ((?=^[0-9a-z.-]{3,256}$)), .:

^((?=^[0-9a-z.-]{3,256}$)[^-].*[^-])$

Regex101

+2

:

^(?!-)[a-z0-9.-]{3,256}(?<!-)$

(?!-) - ( ), (?<!-) - lookbehind ( ).

+1

You do not want {3,256}... You want {1,254}, because [^-]each also corresponds to 1 character at the beginning and end of the line, so you have to subtract them from the total number of characters you want.

^[a-z0-9.][a-z0-9.-]{1,254}[^a-z0-9.]$

Or, if you want to keep your values, you can also use lookahead / behinds:

^(?=[a-z0-9.])[a-z0-9.-]{3,256}(?<=[a-z0-9.])$
0
source

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


All Articles