如何动态生成字符串验证?

时间:2022-11-23 08:40:17

Does anyone know of a library (preferably php) or algorithm for auto-generating regex's from some common descriptions?

有没有人知道一个库(最好是php)或自动生成正则表达式的算法来自一些常见的描述?

For example, have a form with the possible options of:

例如,有一个包含可能选项的表单:

 - Length (=x, between x & y, etc)
  - Starts with
  - Ends with
  - Character(s) x(yz) at index i
  - Specify one or more alternative behavior based on the above
  - And so on..

The idea is that for certain data entities in a system, you'll be able to go to a form and set this criteria for a data field. Afterward, any time that data field for that type of data entity is entered, it will be validated against the regex.

我们的想法是,对于系统中的某些数据实体,您将能够转到表单并为数据字段设置此条件。之后,只要输入该类型数据实体的数据字段,就会对正则表达式进行验证。

This seems like it could grow into a complex problem though, so I'm not expecting anyone to solve it as a whole. Any suggestions are much appreciated.

这似乎可能会成为一个复杂的问题,所以我不希望任何人作为一个整体解决它。任何建议都非常感谢。

1 个解决方案

#1


2  

Would simple globs be enough? For globs it's just a matter of replacing * with .* and adding ^ and $. Or may be Excel-style patterns? It should not be too hard to write a regexp generator for simple rules like this...

简单的球是否足够?对于globs,只需要用*替换*并添加^和$。或者可能是Excel风格的模式?为这样的简单规则编写正则表达式生成器应该不会太难...

My point is, adjust your requirements to simplify the code, and then may be add more features as needed.

我的观点是,调整您的需求以简化代码,然后根据需要添加更多功能。

#1


2  

Would simple globs be enough? For globs it's just a matter of replacing * with .* and adding ^ and $. Or may be Excel-style patterns? It should not be too hard to write a regexp generator for simple rules like this...

简单的球是否足够?对于globs,只需要用*替换*并添加^和$。或者可能是Excel风格的模式?为这样的简单规则编写正则表达式生成器应该不会太难...

My point is, adjust your requirements to simplify the code, and then may be add more features as needed.

我的观点是,调整您的需求以简化代码,然后根据需要添加更多功能。