Search Unity

Question Regex parsing "^[a-zA-Z0-9]{5,20}+$" - Nested quantifier +

Discussion in 'Scripting' started by LoopIssuer, Sep 28, 2022.

  1. LoopIssuer

    LoopIssuer

    Joined:
    Jan 27, 2020
    Posts:
    109
    Hi,
    I'm trying to use regex:

    if (!Regex.Match(name, "^[a-zA-Z0-9]{5,20}+$").Success)


    However, it gives error:
    ArgumentException: parsing "^[a-zA-Z0-9]{5,20}+$" - Nested quantifier +.


    When trying to escape +:
    if (!Regex.Match(name, "^[a-zA-Z0-9]{5,20}//+$").Success)

    it is not working - no error, but every string is invalid.

    Please help.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    You need to surround the expression you want to use the
    +
    symbol on with parentheses like this:

    ^([a-zA-Z0-9]{5,20})+$


    Here's a version of it modified to look for whitespace in case you're trying to support multiple words:

    ^([a-zA-Z0-9]{5,20}[\s]{0,1})+$


    For debugging regular expressions I recommend the following site:

    https://regex101.com/
     
    Last edited: Sep 28, 2022
    Bunny83 likes this.
  3. LoopIssuer

    LoopIssuer

    Joined:
    Jan 27, 2020
    Posts:
    109
    Thank you very much!