I'm building up a library of filters for a validation class in PHP, some of them using regular expressions. I have a lot of filters in mind, but I also don't want to potentially miss any. What do you most often use regular expressions to check? What are some of the not-so-common things that you've had to check that would still be useful in a library? Note: I'm not looking for the actual regex code, just what you use it for.
我正在为PHP中的验证类构建一个过滤器库,其中一些使用正则表达式。我有很多过滤器,但我也不想错过任何过滤器。你最常使用正则表达式来检查什么?你必须检查哪些不太常见的东西在图书馆中仍然有用?注意:我不是在寻找实际的正则表达式代码,而是正在使用它。
7 个解决方案
#1
4
Regex should be strongly tested with their expected use cases. Hence, it may be difficult to develop a complete and general library. I would aim for a library of functions you know you need now. Then add to this list later, when you have proper test cases.
正则表达式应该用他们预期的用例进行强烈测试。因此,开发一个完整的通用库可能很困难。我的目标是建立一个你现在需要的函数库。然后,当您有适当的测试用例时,请稍后添加到此列表中。
That said, here are some common use cases:
也就是说,这里有一些常见的用例:
Numeric Data
Phone numbers
Dates
Zip codes
SSN
数字数据电话号码日期邮政编码SSN
#2
1
so you're looking for the type regular expressions we use for validating?
所以你正在寻找我们用于验证的类型正则表达式?
telephone (various international formats), postal code, zip code, credit card #s, email, dates, digits, ssn, urls (http, ftp, ...)
电话(各种国际格式),邮政编码,邮政编码,信用卡#,电子邮件,日期,数字,ssn,网址(http,ftp,...)
#3
1
In addition to Nescio's answers...
除了Nescio的答案......
- Passwords
- Email addresses
- Disallowing characters various charters in text fields like non-alphanumeric characters
禁止在非字母数字字符等文本字段中使用各种章程字符
#4
1
SQL injection attack patterns
SQL注入攻击模式
'[\s]*--
Password Strength
((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,255})
#5
1
My main uses for regular expression are:
我对正则表达式的主要用途是:
- pulling apart text
- selecting lines in input
- validating formats
- analyzing/sanitizing input
- parsing
- providing expansive customization (allowing "configurable configurations", shortcuts,...)
拉开文本
选择输入行
提供广泛的自定义(允许“可配置的配置”,快捷方式,......)
A number of these things overlap. But it all has to do with human input. Machine readable and human readable are two different things. Regular expressions help us deal with human-oriented (that we know something about) stuff without needing a complete grammar.
其中一些事情重叠。但这一切都与人类的投入有关。机器可读和人类可读是两回事。正则表达式帮助我们处理面向人的(我们知道某些东西)的东西,而不需要完整的语法。
#6
0
The majority of my RE use is fixing up data given to me by various sources into a standardized format. A lot of exporting excel docs as CSV or tab delimited and then running through a bunch of RE transformations in TextPad.
我的RE的大部分用途是将各种来源提供的数据修复为标准化格式。很多人将Excel文档导出为CSV或制表符分隔,然后在TextPad中运行一堆RE转换。
#7
0
Please see Abigail's canonical Regexp::Common.
请参阅阿比盖尔的规范Regexp :: Common。
#1
4
Regex should be strongly tested with their expected use cases. Hence, it may be difficult to develop a complete and general library. I would aim for a library of functions you know you need now. Then add to this list later, when you have proper test cases.
正则表达式应该用他们预期的用例进行强烈测试。因此,开发一个完整的通用库可能很困难。我的目标是建立一个你现在需要的函数库。然后,当您有适当的测试用例时,请稍后添加到此列表中。
That said, here are some common use cases:
也就是说,这里有一些常见的用例:
Numeric Data
Phone numbers
Dates
Zip codes
SSN
数字数据电话号码日期邮政编码SSN
#2
1
so you're looking for the type regular expressions we use for validating?
所以你正在寻找我们用于验证的类型正则表达式?
telephone (various international formats), postal code, zip code, credit card #s, email, dates, digits, ssn, urls (http, ftp, ...)
电话(各种国际格式),邮政编码,邮政编码,信用卡#,电子邮件,日期,数字,ssn,网址(http,ftp,...)
#3
1
In addition to Nescio's answers...
除了Nescio的答案......
- Passwords
- Email addresses
- Disallowing characters various charters in text fields like non-alphanumeric characters
禁止在非字母数字字符等文本字段中使用各种章程字符
#4
1
SQL injection attack patterns
SQL注入攻击模式
'[\s]*--
Password Strength
((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,255})
#5
1
My main uses for regular expression are:
我对正则表达式的主要用途是:
- pulling apart text
- selecting lines in input
- validating formats
- analyzing/sanitizing input
- parsing
- providing expansive customization (allowing "configurable configurations", shortcuts,...)
拉开文本
选择输入行
提供广泛的自定义(允许“可配置的配置”,快捷方式,......)
A number of these things overlap. But it all has to do with human input. Machine readable and human readable are two different things. Regular expressions help us deal with human-oriented (that we know something about) stuff without needing a complete grammar.
其中一些事情重叠。但这一切都与人类的投入有关。机器可读和人类可读是两回事。正则表达式帮助我们处理面向人的(我们知道某些东西)的东西,而不需要完整的语法。
#6
0
The majority of my RE use is fixing up data given to me by various sources into a standardized format. A lot of exporting excel docs as CSV or tab delimited and then running through a bunch of RE transformations in TextPad.
我的RE的大部分用途是将各种来源提供的数据修复为标准化格式。很多人将Excel文档导出为CSV或制表符分隔,然后在TextPad中运行一堆RE转换。
#7
0
Please see Abigail's canonical Regexp::Common.
请参阅阿比盖尔的规范Regexp :: Common。