【问题标题】:Search for a string that contains numbers python搜索包含数字的字符串python
【发布时间】:2018-07-31 19:52:33
【问题描述】:

所以我有以下代码:

cus_str = "show configuration " #intiate router command string variable

with open(var) as config_file: #open file
    for line in config_file: #line by line reading of file
        if '"xe-" + #anynumber + "/" #anynumber + "/" + #anynumber' in line:
            line = line.replace('\n' , '').replace('"' , '').replace(']' , '')     #removes all extra characters
            i = "| except " + (line.split("cust ", 1)[1]) + " " #split the line and save last index (cust name)
        cus_str+=i #append to string
config_file.close()

行:if '"xe-" + #anynumber + "/" #anynumber + "/" + #anynumber' in line: 这就是我在语法方面苦苦挣扎的地方。

我正在查看文件中的一行是否包含以下字符串:“xe-number/number/number”(例如:“xe-6/1/10”)。它将始终具有这种格式,但数字会改变。我会使用什么样的语法来最有效地做到这一点。

谢谢!

【问题讨论】:

标签: python regex string digits


【解决方案1】:

您可以为此使用正则表达式。正则表达式允许您指定文本模式(尽管并非所有模式都可以表示为正则表达式)。然后我们可以将该表达式compile 转换为Pattern 对象,并将该对象用于search 字符串作为模式。

import re

pattern = re.compile(r'"xe-\d+/\d+/\d+"')  # \d+ is "one or more digits".  
                                           # Everything else is literal

with open(var) as config_file:
    for line in config_file:
        if pattern.search(line):  # will return a Match object if found, else None
            ...

【讨论】:

    【解决方案2】:

    听起来像是 Regular Expressions 图书馆的工作!

    这个数字是日期吗?您可以限制位数。

    from re import search, compile #Import the re library with search
    pattern = compile(r"xe-\d{1,2}\/\d{1,2}\/\d{1,2}") #Use this pattern to find lines
    
    cus_str = "show configuration " #intiate router command string variable
    
    with open(var) as config_file: #open file
        for line in config_file: #line by line reading of file
            if search(pattern, line): #Returns None if match is not found
                line = line.replace('\n' , '').replace('"' , '').replace(']' , '')     #removes all extra characters
                i = "| except " + (line.split("cust ", 1)[1]) + " " #split the line and save last index (cust name)
            cus_str+=i #append to string
    

    正则表达式:xe-\d{1,2}\/\d{1,2}\/\d{1,2} 匹配“xe-”后跟 3 组带有斜线分隔符的数字对。每组数字的长度可以是 1 或 2 个字符。

    旁注

    • 您无需关闭 config_file,因为当您退出该块时,with 语句会为您执行此操作。

    • 当模式不匹配时,我不确定您尝试使用 cus_str += i 完成什么。就目前而言,它只会从上一行重复相同的i,除非您将该行缩进 1 级。或者如果第一行不包含该模式,则会给您一个错误。

    【讨论】:

    • 非常漂亮的图表。你是如何产生它的?我认为原始问题包括围绕"" 双引号,您可能需要修改答案。
    • 我认为双引号是伪代码的一部分,用于显示她想要为该行做什么。
    【解决方案3】:

    你可以用一个简单的程序来测试一个正则表达式,比如像https://pythex.org/这样的一些在线工具,然后制作你的程序,一个指向正确方向的小样本是:

    import re
    
    config_file = ["xe-6/1/10", "xf-5/5/542", "xe-4/53/32" ]
    rex = re.compile(r'xe-[\d]+/[\d]+/[\d]+')
    for line in config_file: #line by line reading of file
        if rex.search(line):
          print(line)
    
    xe-6/1/10
    xe-4/53/32
    

    【讨论】:

      猜你喜欢
      • 2021-06-12
      • 2011-05-25
      • 2019-05-17
      • 2013-06-26
      • 2013-11-10
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      • 2020-05-01
      相关资源
      最近更新 更多