siefix
siefix

Reputation: 1016

Firestore Security Rules Regex

I'm trying to evaluate a string in Firestore Security Rules based on the matches regex functionality

my code is username.matches('^(?!\.)(?!_)(?!.*\.$)(?!.*?\.\.)[a-z0-9_.]+$')

Using a regex simulator online it's working

https://regex101.com/r/bDXMg3/2/

But using the same syntax in Security Rules is throwing a ton of errors

enter image description here

I tried to then double escape each .

using the code username.matches('^(?!\\.)(?!_)(?!.*\\.$)(?!.*?\\.\\.)[a-z0-9_.]+$')

It only shows one error (red ^ sign at the beginning), but then it gives me the error below

enter image description here

Invalid regular expression pattern. Pattern: ^(?!\.)(?!_)(?!.*\.$)(?!.*?\.\.)[a-z0-9_.]+$.

My goal is to:

Can anyone let me know what I'm doing wrong?

Upvotes: 6

Views: 3905

Answers (1)

siefix
siefix

Reputation: 1016

Answer is in the link below from Wiktor Stribiżew

If it is really RE2 that is parsing this pattern, then it is clear - it does not support lookaheads/lookbehinds.

Use '^[a-z0-9][a-z0-9_]*([.][a-z0-9_]+)*$'

Google RE2 Regex Escaping periods and underscores error

Upvotes: 5

Related Questions