gpt4 book ai didi

regex - 什么是可以测试字符串是否为有效 Firebase key 的正则表达式?

转载 作者:行者123 更新时间:2023-12-03 23:24:36 26 4
gpt4 key购买 nike

我正在编写一个应用程序,允许用户输入将作为键存储在 Firebase 中的值。

Firebase key 要求:

max size - 768 bytes
cannot contain . $ # [ ] / or ASCII control characters 0-31 or 127
allows single spaces, but not double or more spaces

如何将其表示为正则表达式?

最佳答案

假设 1 个字节 = 1 个字符,并且由于您提到了 ASCII,因此假设有效字符是 ASCII 字符 32 到 126。

“匹配任何这些允许的字符,正好 768 次”:

[ !"%&'()*+\,\-\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz{|}]{768}

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

编辑

那没有用,因为我错过了阻止连续空格的需要。新建议,基本模式为:
# a space, not followed by a space
# or a character not followed by a double-space.
# This pattern, matched n times, locking the start and end of the string.

^( (?! )|[a](?! )){5}$

但是,当我在...中替换完整字符集时
^( (?! )|[ !"%&'()*+\,\-\/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\^_`abcdefghijklmnopqrstuvwxyz{|}](?!  )){1,768}$

# it breaks Regex101, saying it's too large.

注意。没有 RegEx 就容易多了:

# Python example validator

def validate(s):
valid_chars = 'abc123...'

if not (0 < len(s) <= 768): return False
if ' ' in s: return False
for char in s: if char not in valid_chars: return False

return True

关于regex - 什么是可以测试字符串是否为有效 Firebase key 的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30627101/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com