簡體   English   中英

在python中使用正則表達式搜索三個不同的詞

[英]searching three different words using regex in python

我正在嘗試在以下輸出中搜索三個不同的詞

+---------------------+---------------------------+------------------+------------+-----------+------------+----------+-------------------+----------------+
|    radius-server    |           address         |      secret      |  auth-port |  acc-port |  max-retry |  timeout |    nas-ip-local   |  max-out-trans |
+---------------------+---------------------------+------------------+------------+-----------+------------+----------+-------------------+----------------+
|              rad_11 |                 127.0.0.1 |       testing123 |       9812 |      9813 |          5 |       10 |           disable |            200 |
+---------------------+---------------------------+------------------+------------+-----------+------------+----------+-------------------+----------------+

他們是rad_11127.0.0.1testing123 有人可以幫我嗎 ?

我已經嘗試過re.search ('rad_11' '127.0.0.1' 'testing123', output)

您可以清除所有不必要的符號並解析字符串:

import re

new_string = re.sub('\+[\-]*|\n', '', a).strip(' |').replace('||', '|')

names_values = map(lambda x: x.strip(' |\n'), filter(bool, new_string.split(' | ')))

count_of_values = len(names_values)/2
names, values = names_values[:count_of_values], names_values[count_of_values:]
print dict(zip(names, values))

>>> {'max-out-trans': '200', 'nas-ip-local': 'disable', 'address': '127.0.0.1', 
 'radius-server': 'rad_11', 'secret': 'testing123', 'acc-port': '9813', 
 'timeout': '10', 'auth-port': '9812', 'max-retry': '5'}

要匹配任何模式,可以使用re.findall()

import re
>>> string = "+---------------------+---------------------------+------------------+------------+-----------+------------+----------+-------------------+----------------+ | radius-server | address | secret | auth-port | acc-port | max-retry | timeout | nas-ip-local | max-out-trans | +---------------------+---------------------------+------------------+------------+-----------+------------+----------+-------------------+----------------+ | rad_11 | 127.0.0.1 | testing123 | 9812 | 9813 | 5 | 10 | disable | 200 | +---------------------+---------------------------+------------------+------------+-----------+------------+----------+-------------------+----------------+"
>>> print re.findall(r'rad_11|127\.0\.0\.1|testing123', string)
>>> ['rad_11', '127.0.0.1', 'testing123']

搜索所有模式要簡單得多:

def all_exists(string, patterns):
    for pattern in patterns:
        if pattern not in string:
            return False
    return True

>>> print all_exists('aaa bbb ccc', ['aaa', 'bbb'])
True
>>> print all_exists('aaa bbb ccc', ['aaa', 'bbb', 'ddd'])
False

來自re.findall()文檔

返回字符串中模式的所有非重疊匹配項,作為字符串列表。 從左到右掃描該字符串,並以找到的順序返回匹配項。 如果該模式中存在一個或多個組,則返回一個組列表;否則,返回一個列表。 如果模式包含多個組,則這將是一個元組列表。 空匹配項將包括在結果中,除非它們碰到另一個匹配項的開頭。

您可以隨時使用:

>>> if 'rad_11' in string and '127.0.0.1' in string and 'testing123' in string:
...  print "Got all 3"
... else:
...  print "Failed - all 3 not present"
... 
Got all 3
>>> if 'rad_11' in string and '127.0.0.2' in string and 'testing123' in string:
...  print "Got all 3"
... else:
...  print "Failed - all 3 not present"
... 
Failed - all 3 not present

它不花哨,但很清楚,可以完成工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM