Reputation: 1016
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
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
Invalid regular expression pattern. Pattern: ^(?!\.)(?!_)(?!.*\.$)(?!.*?\.\.)[a-z0-9_.]+$.
My goal is to:
.
or _
.
.
in a rowletter characters
and numbers
Upvotes: 6
Views: 3905
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