Kenny
Kenny

Reputation: 2166

How to validate that a string contains only digits and hyphens?

I need to create a regex that will meet the following criteria:

Valid characters: 0-9 and hyphen (-) only. Entry must be between 11 and 13 characters. Also, must not contain the string "73480"

Upvotes: -2

Views: 107

Answers (2)

Andy Lester
Andy Lester

Reputation: 93636

You want two regexes for clarity.

First check that this matches:

/^[-\d]{11,13}$/

and then check for failing to match:

/73480/

In Perl you'd do this like /^[-\d]{11,13}$/ && !/73480/. If this was PHP, you'd make two calls to preg_match.

Trying to cram it all into one regex makes things too hard to read in the future.

Upvotes: 0

Ωmega
Ωmega

Reputation: 43673

Use regex pattern ^(?!.*73480)[0-9-]{11,13}$

Upvotes: 6

Related Questions