简体   繁体   中英

How to design regex pattern for number?

I am working on regex pattern to validate input, but I am not sure how to include () and - symbols.

Description: The input string doesn't have to be filled out. If its filled out it needs to have exactly 5 numbers and input can't start with 26. Also, input needs to accept parenthesis and dash and they can be placed in any part of the input. I accept max 10 characters

Tried:

(^(?!26)((\d){5}))?

Works only for: - empty input - exactly ten digit number for example 0123456789 - also it rejects if you have 26 at the beginning, for example 2651234567

Also, tried to include - and () but this pattern doesn't work at all

(^(?!26)(\-\(\))((\d){5}))?

Valid inputs:
(12345)--
---12)567)
12333
-1-2--3-45
(()))12345
((12345
(-)65768-
(4)1-2-35

Invalid inputs:
26123---
(26)897---
-26333----
26
26(((())
26------
26--345
26)88-76
267-9

I found discussion A comprehensive regex for phone number validation and it helped but I still can't match exactly my entry.

You're using a Java flavor regex. The correct regex pattern would be:

^(?!26)([-()]*\\d){5}[-()]*$

This one will make it so your input cannot start with 26. However, your post did not specify if it could be something like --2)6-218 (it doesn't start with 26, however the first two digits are 26. If this were the case, then you would need:

^(?![-()]*2[-()]*6)([-()]*\\d){5}[-()]*$

The 10 character max should be validated on the input, maxlength=10 .

Edit: as @zx81 pointed out, I had a few unnecessary escapes. I don't know what I was thinking, sorry. However, this regex pattern does not accept empty strings.

This regex will do what you want (see demo ):

^(?!\D*(?:26|555))(?=(?:\D*\d){5}\D*$)[\d()-]{5,10}$

If you no longer want to reject 555 , you can go with:

^(?!\D*26)(?=(?:\D*\d){5}\D*$)[\d()-]{5,10}$

And if 2--6123 is not allowed, change the regex to

^(?!\\D*2[()-]*6)(?=(?:\\D*\\d){5}\\D*$)[\\d()-]{5,10}$

Explain Regex

^                        # the beginning of the string
(?!                      # look ahead to see if there is not:
  \D*                    #   non-digits (all but 0-9) (0 or more
                         #   times (matching the most amount
                         #   possible))
  (?:                    #   group, but do not capture:
    26                   #     '26'
   |                     #    OR
    555                  #     '555'
  )                      #   end of grouping
)                        # end of look-ahead
(?=                      # look ahead to see if there is:
  (?:                    #   group, but do not capture (5 times):
    \D*                  #     non-digits (all but 0-9) (0 or more
                         #     times (matching the most amount
                         #     possible))
    \d                   #     digits (0-9)
  ){5}                   #   end of grouping
  \D*                    #   non-digits (all but 0-9) (0 or more
                         #   times (matching the most amount
                         #   possible))
  $                      #   before an optional \n, and the end of
                         #   the string
)                        # end of look-ahead
[\d()-]{5,10}            # any character of: digits (0-9), '(', ')',
                         # '-' (between 5 and 10 times (matching the
                         # most amount possible))
$                        # before an optional \n, and the end of the
                         # string

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM