如何在Python3中通过for循环获得严格的单词匹配

2024-05-19 08:36:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面的代码片段中搜索wordkoint,但是它会打印所有相关的单词,比如kointkoint_localkoint_limited。你知道吗

除了regex之外,是否有一种严格的方法来查找这类需求中的字符串/单词。你知道吗

from subprocess import Popen, PIPE

CRED = '\033[91m'
CGRN = '\033[92m'
CEND = '\033[0m'

with open("kkdiff", "r") as lid:
    for line in lid:
        line = line.strip()
        proc = Popen(['id', line], stdout=PIPE,)
        myID = proc.communicate()[0].decode('utf-8')
        if 'koint' in myID:
            print(line, CGRN + "Success: " + CEND + "User exists in the Group")
        else:
            print(line, CRED + "Failed: " + CEND + "User does not exists in the Group")

当我只查找koint时,上面的代码段返回如下:

user1 Failed: User does not exists in the Group
user30 Success: User exists in the Group (<-- koint_local)
user81 Success: User exists in the Group  (<-- koint_limited)

Raw data in myID:

uid=24699(user1) gid=1001(skilla) groups=1786(koint),1614(koint_limited),101(torr)

在上面的数据中,即使koint缺失并且koint_limited存在,也表示成功。你知道吗

要求:如果在行中找到koint,则打印成功。你知道吗

正如我在文章开头提到的,除了regex: 尽管正则表达式有效:

from subprocess import Popen, PIPE

CRED = '\033[91m'
CGRN = '\033[92m'
CEND = '\033[0m'

with open("kkdiff", "r") as lid:
    for line in lid:
        line = line.strip()
        proc = Popen(['id', line], stdout=PIPE,)
        myID = proc.communicate()[0].decode('utf-8')
        if re.search(r'\bkoint\b', myID):
            print(line, CGRN + "Success: " + CEND + "User exists in the Group")
        else:
            print(line, CRED + "Failed: " + CEND + "User does not exists in the Group")

Tags: theinexistslinegrouppopenpipecred
2条回答

我已经编译了工作代码,所以它可能是有用的人看所以。现在有两种有regex和没有regex的解决方案。你知道吗

1)不含regex

from subprocess import Popen, PIPE, DEVNULL
##### color for success & Failed code ########
CRED = '\033[91m'
CGRN = '\033[92m'
CEND = '\033[0m'
###############################################
with open(input("Please Enter the userfile: "), "r") as lid:
    for line in lid:
        line = line.strip()
        proc = Popen(['groups', line], stdout=PIPE, stderr=DEVNULL)
        myID = proc.communicate()[0].decode('utf-8')
        if 'koint' in myID.split():
            print(line, CGRN + "Success: " + CEND + "user exists in the group")
        else:
            print(line, CRED + "Failed: " + CEND + "user doesn't exists in the group")

2)基于regex

    from subprocess import Popen, PIPE, DEVNULL
    import re
    ##### color for success & Failed code ########
    CRED = '\033[91m'
    CGRN = '\033[92m'
    CEND = '\033[0m'
   ################################################
    with open(input("Please Enter the userfile: "), "r") as lid:
        for line in lid:
            line = line.strip()
            proc = Popen(['id', line], stdout=PIPE, stderr=DEVNULL)
            myID = proc.communicate()[0].decode('utf-8')
            if re.search(r'\bkoint\b', myID):
                print(line, CGRN + "Success: " + CEND + "User exists in the Group")
            else:
                print(line, CRED + "Failed: " + CEND + "User does not exists in the Group")

将字符串与完整单词koint匹配的正则表达式

>>> re.search(r'\bkoint\b', 'groups=1786(koint)') is not None
True
>>> re.search(r'\bkoint\b', '1614(koint_limited)') is not None
False

Regular Expression Syntax


看起来groups命令更适合您的需要。你知道吗

>>> proc = subprocess.Popen(['groups', line], stdout=PIPE)
>>> myID = proc.communicate()[0].decode('utf-8')
>>> 'koint' in myID.split()

相关问题 更多 >

    热门问题