在使用python的正则表达式时,对一些配置文件进行查找匹配。总觉得自己写的不是最优,但又可用。
比如如下
interface Vlan100
ip address 10.212.44.129 255.255.255.128 secondary
目的是匹配出IP地址和掩码来,其中IP地址和掩码均不固定。
^sipsaddresss(?:d+.){3}d+s(?:d+.){3}d+ssecondary
我使用上面的表达式可以得到结果,然后做一些split之类的得到IP地址和掩码。
但作为一个强迫症来说,总觉得不够完美。请教下如何判断自己的表达式是最优的(不限于上面这个,上面的只是举例)
PHP中文网2017-04-17 17:57:46
Visual inspection of your data is very standardized and regular, so there will be no abnormal data, then simple regular matching will suffice
address\s([\d.]+)\s([\d.]+)\ssecondary
The first subexpression is ip, and the second subexpression is mask.
I don’t know python, so I can’t write you a py example, but this regular expression is universal.
ringa_lee2017-04-17 17:57:46
In the end, just generate enough random test sets and pass them. . .