how to allow only one caret symbol between acceptable inputs using regex

I want to check some of the acceptable inputs using regex. following is the list of acceptable values

Acceptable Values :12345, VS12345, 12345^67890, VS12345^VS67890, VSGC1234

Also i want check if special characters are entered more than one as only one caret is allowed.

1 answer

  • answered 2021-09-23 13:59 Niel Godfrey Ponciano

    Use a negative lookahead (?!.*\^.*\^) to ensure that the string to capture doesn't contain more than 1 caret ^

    ^(?!.*\^.*\^)[\^\dA-Z]*$
    

    Or if your only concern is to not catch multiple caret symbols and allow anything else:

    ^[^\^]*\^?[^\^]*$
    

    enter image description here

    Where:

    • ^ - Start of string
    • (?!.*\^.*\^) - Match the pattern only if the next characters doesn't contain 2 or more caret ^ symbols
    • [\^\dA-Z]* - Match any caret, digit, and capital letter. With the previous assertion, it will be sure that this will not contain more than 2 caret symbols.
    • $ - End of string

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum