首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果匹配可以是单个字符串或字符串元组,如何测试字符串中是否存在匹配?

如果匹配可以是单个字符串或字符串元组,如何测试字符串中是否存在匹配?
EN

Stack Overflow用户
提问于 2015-02-12 07:26:41
回答 1查看 48关注 0票数 1

我正在尝试从文本文件中直接进行字符串匹配。有时,匹配意味着一个字符串包含多个目标字符串。目前,我的代码看起来就像

代码语言:javascript
复制
interesting_matches = [
    "sys/bootdisk.py",
    " engine stalled for ",
    " changed to stalled)",
    "DSR failure",
    "Detected IDI failure",
    "idi_shallow_verify_failure",
    "Malformed block history",
    "Out of order sequence message on",
    "Port reset timeout of",
    "gmp_info",
    "test_thread",
    " panic @ time ",
    ": *** FAILED ASSERTION",
    "filesystem full",]

    for match in interesting_matches:
        # Iterate through simple matches.
        if match in line:
            processed_line_data = self._process_line(
                match,
                line,
                line_datetime,
                line_num,
                current_version)

    if "kern_sig" in line and "pid" in line:
        processed_line_data = self._process_line(
            ("kern_sig", "pid"),
            line,
            line_datetime,
            line_num,
            current_version)

    if "vfs_export" in line and "ignoring" in line:
        processed_line_data = self._process_line(
            ("vfs_export", "ignoring"),
            line,
            line_datetime,
            line_num,
            current_version)

    if "job_d" in line and
        "State transition from state " in line and
        " took longer than " in line:
        processed_line_data = self._process_line(
            (
                "job_d",
                "state transition from state",
                " took longer than "),
            line,
            line_datetime,
            line_num,
            current_version)

    if processed_line_data is not None:
        return_list.append(processed_line_data)

我想做的是类似于

代码语言:javascript
复制
interesting_matches = [
        "sys/bootdisk.py",
        " engine stalled for ",
        " changed to stalled)",
        "DSR failure",
        "Detected IDI failure",
        "idi_shallow_verify_failure",
        "Malformed block history",
        "Out of order sequence message on",
        "Port reset timeout of",
        "gmp_info",
        "test_thread",
        " panic @ time ",
        ": *** FAILED ASSERTION",
        "filesystem full",
        ("kern_sig", "pid"),
        ("vfs_export", "ignoring"),
        ("job_d", "State transition from state", " took longer than "),]

for matches in interesting_matches
     if any(match in line for match in matches):
           processed_line_data = self._process_line(
               match,
               line,
               line_datetime,
               line_num,
               current_version)

但是元组和字符串的混合会导致值错误,说明不能比较字符串和元组。

如果要检查单个字符串和多个字符串,如何编写单个比较?

编辑:

以下是基于肖恩答案的工作代码

代码语言:javascript
复制
interesting_matches = [
    ("sys/bootdisk.py",),
    (" engine stalled for ",),
    (" changed to stalled)",),
    ("DSR failure",),
    ("Detected IDI failure",),
    ("idi_shallow_verify_failure",),
    ("Malformed block history",),
    ("Out of order sequence message on",),
    ("Port reset timeout of",),
    ("gmp_info",),
    ("test_thread",),
    (" panic @ time ",),
    (": *** FAILED ASSERTION",),
    ("filesystem full",),
    ("kern_sig", "pid"),
    ("vfs_export", "ignoring"),
    ("job_d", "State transition from state", " took longer than "),]

for matches in interesting_matches:
    if all(match in "test_thread" for match in matches):
        print(matches)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-12 07:44:51

非常可行。尝试如下:对于单个子字符串签名,无论如何,将它们包装在一个元组中。现在,您的列表是同构的,您的问题变得更简单了。对于每个签名元组,请检查签名中的所有子字符串是否也在行中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28471637

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档