Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6967411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:19:05+00:00 2026-05-27T16:19:05+00:00

I need a Regular Expression allowing user to input numbers, plus, minus and parentheses.

  • 0

I need a Regular Expression allowing user to input numbers, plus, minus and parentheses.

User can only input:

  • At most one open parenthesis ‘(‘.
  • At most one close parenthesis ‘)’.
  • At most one plus ‘+’
  • As many minus ‘-‘ but not after each other.
  • Exactly 11 numbers.

Here are valid inputs:

(0)+12-3-4-56-7890
+)0(12345-678-90
+01234567890
+(01234567890)
01234567890
-01-234+5678-90
(01234567890)
)01234567890(

And following are not valid:

0123456--7890
0((1234567890
01234567890))
++01234567890
123456
++123456789

I’m using C# for programming and if it helps order of open and close parentheses can become mandatory too. so )01234567890( will not be valid.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:19:06+00:00Added an answer on May 27, 2026 at 4:19 pm

    This regex passes your examples, but might not be exactly what you’re looking for. It should point you in the right direction.

    ^(?!.*-{2,})(?!(?:.*\)){2,})(?!(?:.*\(){2,})(?!\+{2,})(?:\D*\d\D*){11}$
    
    • (?!.*-{2,}) Cannot contain two or more hyphens.
    • (?!(?:.*)){2,}) Cannot contain two or more closing parentheses.
    • (?!(?:.*(){2,}) Cannot contain two or more opening parentheses.
    • (?!+{2,}) Cannot start with more than two addition symbols.
    • (?:\D*\d\D*){11} Must contain 11 instances of a numeric character surrounded by anything.

    However, this is very confusing and fairly inefficient. I bet the regex could be rewritten to be much quicker, but won’t be much easier to understand.

    I suggest that you follow MisterJack’s suggestion instead of pursue a regex. It’ll be easier to maintain.

    EDIT


    ^(?!.*--)(?!.*(\(|\)|\+).*\1)(?:\D*\d\D*){11}$
    

    I’ve consolidated the parentheses and plus symbol rules into one negative lookahead using a backreference. This also restricts the number of parens and pluses to just one of each. I couldn’t get it to restrict to just a certain set of characters, but you might be able to do that in a second pass with another regex.

    • ^ Match from beginning of the string
    • (?!.*–) Do not allow consecutive hyphens
    • (?!.* ((|)|+).*\1) Do not allow two or more instances of () or +
    • (?:\D*\d\D*){11} Must contain 11 digits, allow non-digit characters before and after, such as hyphen.
    • $ Match to end of string

    I tried a negative and positive lookahead to restrict the characters, but couldn’t get it to work right. I also tried to replace \D with [()+-] but that didn’t work either. Maybe someone else will add a comment to show how to restrict the characters. I’d sure love to see how someone else does it in this regex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a regular expression pattern to only accept positive whole numbers. It can
I need regular expression to match braces correct e.g for every open one close
I need a regular expression for validation two or one numbers then , or
Could you tell how to replace string by preg-replace (need regular expression): /user/{parent_id}/{action}/step/1 At
I need a regular expression to validate string with one or more of these
I Need a regular expression which accepts all types of characters (alphabets, numbers and
i need a regular expression validation for numeric digits grouped as X-XXXXX-XXX-X can any
I need a Perl regular expression to match a string. I'm assuming only double-quoted
I need a regular expression that I can use in VBScript and .NET that
I need a regular expression to allow the user to enter an alphanumeric string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.