0

我已将命令的输出存储chage -l user在变量中output,需要检查用户帐户密码是否未过期或将在 90 天内过期。

import re

output = '''Last password change                                    : Aug 26, 2017
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7
'''

regexp = re.compile(r'Password expires [ ]*(:*?)')
match = regexp.match(output)
if match:
    VALUE = match.group(2)

现在,我需要将值存储在一个变量中以继续前进但无法做到这一点。以上是我的代码。理想情况下,VALUE 应该有“从不”。

4

1 回答 1

1

re.match不会在整个字符串中查找模式,而是会在字符串的开头匹配它(就像正则表达式以 开头一样^)。所以你需要re.search,它将检查整个目标字符串的模式:

import re
output = '''Last password change                                    : Aug 26, 2017
Password expires                                        : never
Password inactive                                       : never
Account expires                                         : never
Minimum number of days between password change          : 0
Maximum number of days between password change          : 99999
Number of days of warning before password expires       : 7
'''

regexp = re.compile(r'Password expires\s+: (.*)')
match = regexp.search(output)
if match:
    VALUE = match.group(1)
    print(VALUE)
于 2019-04-23T07:00:27.390 回答