Search code examples
iosregexazure-ad-b2cregex-lookarounds

Regex for iOS Bundle ID in platform for application registered in Azure B2C


I need a regex with max 222 chars

  • must use a-z A-Z 0-9
  • can also have . and - but:
  • can not start from . or -
  • can not end with . or -
  • can not have multiple like ".." or "--"
  • can not have only . or -

Examples:

GOOD:

asd.asd.asd.asd.asd.aadas.asdasdasd

as-d.asd.a-sd

BAD:

.asd.asd

-asd.-asd

.-asd.asd

asd.asd.

asd.asd-

asd.asd.-

asd.asd-.

asd.asd--

asd..

asd.asd-.asd

.-

asd--asd..asd

I ended up with something like this ^[^-.]((?!--)[a-zA-Z0-9\-]\.?[^-]){1,100}[^-.]$ but it does not cover this case like asd.asd-.asd

this is regex for platform iOS Bundle ID in app registration. enter image description here


Solution

  • You might write it without the hyphen in the character class [a-zA-Z0-9\-]

    (Note that if the - is at the start or end, you don't have to escape it)

    ^(?=.{1,100}$)[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*$
    
    • ^ Start of string
    • (?=.{1,100}$) Assert 1-100 chars
    • [a-zA-Z0-9]+ Repeat 1+ times matching any of the listed
    • (?:[-.][a-zA-Z0-9]+)* Optionally repeat either - or . and repeat 1+ times any of the listed
    • $ End of string

    Regex demo