Amir Rahbaran
Amir Rahbaran

Reputation: 2430

Regex: Minimum 13 characters and WITHIN 13 characters maximum 3 characters

I need an expression that validates, something like this:

1234-567.890-123

My solution [0-9]{13,}[-\.]{0,3}can't work as this only validates:

1234567890123.-.

Upvotes: 0

Views: 321

Answers (1)

rock321987
rock321987

Reputation: 11042

If ordering of characters in string do not matters, you can use

^(?=(?:[\d.-]){13,}$)(?=(?:\D*\d){13,})(?!(?:[^.-]*[.-]){4}).*$

Regex Breakdown

^ #Starting of string
 (?=(?:[\d.-]){13,}$) #Lookahead which sees that the string only contains digits, . and -
 (?=(?:\D*\d){13,}) #Lookahead to match 13 digits
 (?!(?:[^.-]*[.-]){4}) #Looahead so that the number of . and - are not equal to 4
 .*
$

Middle Part

(?= #Lookahead
   (?:\D*\d) #Match all non-digits and then digit.
   {13,} #Repeat this at least 13 times.In simple words, it is matching at least 13 digits.
)

Upvotes: 1

Related Questions