用于匹配不能包含某些字符或子字符串的字符串的正则表达式
Regex for matching a string that can not have some characters or a substring
我正在尝试根据 Mongo DB specification (mongo 3.6) 为 Mongo 数据库集合名称添加路由时间(在 REST API 建模期间)正则表达式验证.
它说(从上面的文档复制粘贴):
Restriction on Collection Names
Collection names should begin with
- an underscore or a letter character
and cannot:
- contain the $.
- be an empty string (e.g. "").
- contain the null character.
- begin with the system. prefix. (Reserved for internal use.)
我的另一个限制(某种程度上)是:根据 json-schema supported subset of the regexp,它用于 JSON 架构验证。它不是完整的正则表达式(例如,我不能使用 \d、\w(或者我不能使用 \b<...>\b)。
有了这个,到目前为止,我可以完成其他部分 而无需
- begin with the system. prefix. (Reserved for internal use.)
节。
这是我的 REST API JSON 模式中的正则表达式(请参阅下面的模式):
'collectionName': {
description: "foo bar",
type: 'string',
minLength: 1,
maxLength: 120,
pattern: '(^[a-zA-Z0-9_][^$ \0]*$)', <== this one.
example: 'MyCollection',
},
用几个例子进一步说明:
- 它不应该匹配类似-collection的东西,但是应该匹配_collection或collec-tion
- The stings contains a space or $ - 不应该匹配 (e.g. collection, collec$$tion)
- 像这样的东西应该匹配:systemCollection
- 这 不应该匹配 : system.collection // 作为系统。前缀为 Mongo.
保留
尽我所能以最好的方式澄清问题。
在这方面的任何帮助将不胜感激。
普拉迪普
你需要一个消极的前瞻。
^(?!system\.)[a-zA-Z0-9_][^$ \0]*$
(?!...)
Starting at the current position in the expression, ensures
that the given pattern will not match. Does not consume characters. - regex101.com
这在 PCRE 中与 ECMAScript 相同。 MongoDB 对其正则表达式使用 PCRE,因此我假设 JSON 架构中的正则表达式对于 MongoDB.
也是如此
这是一个使用示例数据测试用例的示例 https://regex101.com/r/CHu6lz/1
我只讨论了您所说的不以 system.
开头的情况。您需要扩展正则表达式以添加您的额外案例,但现在您应该清楚如何做到这一点,因为您知道负先行。
您还需要转义正则表达式以在 JSON 中使用,根据对此答案的评论,正如您已经完成的那样。
我正在尝试根据 Mongo DB specification (mongo 3.6) 为 Mongo 数据库集合名称添加路由时间(在 REST API 建模期间)正则表达式验证.
它说(从上面的文档复制粘贴):
Restriction on Collection Names
Collection names should begin with
- an underscore or a letter character
and cannot:
- contain the $.
- be an empty string (e.g. "").
- contain the null character.
- begin with the system. prefix. (Reserved for internal use.)
我的另一个限制(某种程度上)是:根据 json-schema supported subset of the regexp,它用于 JSON 架构验证。它不是完整的正则表达式(例如,我不能使用 \d、\w(或者我不能使用 \b<...>\b)。
有了这个,到目前为止,我可以完成其他部分 而无需
- begin with the system. prefix. (Reserved for internal use.)
节。
这是我的 REST API JSON 模式中的正则表达式(请参阅下面的模式):
'collectionName': {
description: "foo bar",
type: 'string',
minLength: 1,
maxLength: 120,
pattern: '(^[a-zA-Z0-9_][^$ \0]*$)', <== this one.
example: 'MyCollection',
},
用几个例子进一步说明:
- 它不应该匹配类似-collection的东西,但是应该匹配_collection或collec-tion
- The stings contains a space or $ - 不应该匹配 (e.g. collection, collec$$tion)
- 像这样的东西应该匹配:systemCollection
- 这 不应该匹配 : system.collection // 作为系统。前缀为 Mongo. 保留
尽我所能以最好的方式澄清问题。
在这方面的任何帮助将不胜感激。
普拉迪普
你需要一个消极的前瞻。
^(?!system\.)[a-zA-Z0-9_][^$ \0]*$
(?!...)
Starting at the current position in the expression, ensures that the given pattern will not match. Does not consume characters. - regex101.com
这在 PCRE 中与 ECMAScript 相同。 MongoDB 对其正则表达式使用 PCRE,因此我假设 JSON 架构中的正则表达式对于 MongoDB.
也是如此这是一个使用示例数据测试用例的示例 https://regex101.com/r/CHu6lz/1
我只讨论了您所说的不以 system.
开头的情况。您需要扩展正则表达式以添加您的额外案例,但现在您应该清楚如何做到这一点,因为您知道负先行。
您还需要转义正则表达式以在 JSON 中使用,根据对此答案的评论,正如您已经完成的那样。