Python - 理解正则表达式 [英] Python - Understanding Regular Expression
问题描述
所以,我从学校的 Linux 服务器获取用户名列表,上面的代码打开保存它们的目录并将其保存为信息
So, I'm taking a list of usernames from a Linux server at school, this top code opens the directory where they are kept and saves it as information
#!/usr/bin/env python
import subprocess, sys
r = subprocess.Popen(['ls','/home/ADILSTU'], stdout=subprocess.PIPE)
information = r.stdout.read()
print information, str(information)
这很好用,并像这样列出用户......每行列出 1 个用户.(至少有 100 个用户名)
that works just fine and list the users like this... where it list them 1 per line. (there is atleast 100 usernames)
ajax2
jjape3
jaxe32
我的问题是,我想为这些用户名创建一个查找",这是我搜索仅以字母 j 开头的用户名的代码(所以应该只从这个列表中列出 jaxe32)
my problem is, I want to create a "look-up" for these usernames, this is my code to search for usernames that only start with the letter j (so should only list jaxe32 from this list)
#lookup
import re
p = re.compile(r'j(?!j)\w*')
print p.match(str(information)).group()
但是当我运行它时,我得到了这个错误,如果我摆脱了 .group() ,那么它只会指出无",但没有错误.所以我不确定列表是否正确保存到字符串中,或者我是否只是遗漏了一些明显的东西.我只想为此使用正则表达式,而不是其他任何东西.
but when I run this I get this error, and if I get rid of .group() it then just states "none", but no error. So i'm not sure if the list is getting saved to a string right, or if I'm just missing something obvious. I only want to use regular expression for this, not anything else.
Traceback (most recent call last):
File "getInformation.py", line 11, in <module>
print p.match(str(information)).group()
AttributeError: 'NoneType' object has no attribute 'group'
推荐答案
来自 re.match
:
如果字符串开头的零个或多个字符与正则表达式模式匹配,则返回相应的匹配对象.如果字符串与模式不匹配,则返回 None;
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object. Return None if the string does not match the pattern;
re.match
仅当匹配从字符串的开头开始时才有用,它不会在字符串中找到所有匹配项.
re.match
is only useful if the match starts from the beginning of the string, it does not find all matches in a string.
这让您有两个主要选择:
This leaves you with two main options:
按行拆分输入文件并使用
re.match
使用多行匹配和re.findall
选项 1:
r = subprocess.Popen(['ls', '/home/administrator/sotest'], stdout=subprocess.PIPE)
information = r.stdout.read().decode('utf-8').split('\n') # ['ajax2', 'jaxe32', 'jjape3', '']
for user in information:
s = re.match(r'j(?!j)\w*', user)
if s:
print(s.group())
输出:
jaxe32
选项 2(使用 (?m)^j(?!j)\w*$
):
r = subprocess.Popen(['ls', '/home/administrator/sotest'], stdout=subprocess.PIPE)
information = r.stdout.read().decode('utf-8') # 'ajax2\njaxe32\njjape3\n'
print(re.findall(r'(?m)^j(?!j)\w*$', information))
输出:
['jaxe32']
这篇关于Python - 理解正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!