/etc/passwd 中行的正则表达式验证 [英] Regex Verification of Line in /etc/passwd
本文介绍了/etc/passwd 中行的正则表达式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要验证/etc/passwd 文件是否有效,并且认为正则表达式是验证非注释行的好主意.我将如何验证如下一行:
root:*:0:0:系统管理员:/var/root:/bin/sh
经过研究,第 5 个字段(系统管理员)可以包含电子邮件和地址等其他数据,第二个字段可以包含除 :
之外的任何内容,最后 2 个字段是完整路径.>
我将如何为此创建正则表达式的任何线索?
解决方案
类似的事情?
^(#.*|[az]*:[^:]*:[0-9]*:[0-9]*:[^:]*:/[^:]*:/[^:]*)$
(假设用户名由小写字母组成)
I need to verify an /etc/passwd file is valid, and thought regex would be a good idea to verify the lines that are not comments. How would I verify a line like:
root:*:0:0:System Administrator:/var/root:/bin/sh
After some research, the 5th field (System administrator) can contain other data like email and address, the second field could contain anything but a :
, the last 2 fields are full paths.
Any clues how I would create a regex expression for this?
解决方案
Something like this?
^(#.*|[a-z]*:[^:]*:[0-9]*:[0-9]*:[^:]*:/[^:]*:/[^:]*)$
(assuming that the username consists of lowercase letters)
这篇关于/etc/passwd 中行的正则表达式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文