Python 搜索包含数字的字符串

Python 搜索包含数字的字符串,python,regex,string,digits,Python,Regex,String,Digits,因此,我有以下代码: 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 + "/" + #anynumbe

因此,我有以下代码:

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'是我在语法方面遇到的困难

我正在查看文件中的一行是否包含以下字符串:xe number/number/number ex:xe-6/1/10。它将始终采用这种格式,但数字将发生变化。我将使用哪种语法来最有效地执行此操作


谢谢

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

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
            ...

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

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
            ...
听起来像是图书馆的工作

这个号码是约会吗?您可以限制位数

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_文件,因为当您退出块时,语句会为您关闭config_文件

我不确定在没有与模式匹配的情况下,您试图用cus_str+=I实现什么。目前,它只会重复上一行中相同的i,除非将该行缩进1级。或者,如果第一行不包含模式,则会给您一个错误

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
            ...
听起来像是图书馆的工作

这个号码是约会吗?您可以限制位数

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_文件,因为当您退出块时,语句会为您关闭config_文件

我不确定在没有与模式匹配的情况下,您试图用cus_str+=I实现什么。目前,它只会重复上一行中相同的i,除非将该行缩进1级。或者,如果第一行不包含模式,则会给您一个错误

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
            ...

你可以用一个简单的程序来测试一个正则表达式,比如使用一些在线工具,然后制作你的程序,一个小示例可以为你指明正确的方向:

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

你可以用一个简单的程序来测试一个正则表达式,比如使用一些在线工具,然后制作你的程序,一个小示例可以为你指明正确的方向:

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

使用python的正则表达式模块re:使用python的正则表达式模块re:非常好的图表。你是怎么产生的?我认为原来的问题包含了双引号,你可能想修改你的答案。我认为双引号是伪代码的一部分,用来显示她想为那行做什么。非常好的图表。你是怎么产生的?我认为原来的问题包含了双引号,你可能想修改你的答案。我认为双引号是伪代码的一部分,以显示她想为那行做什么。