简体   繁体   English

在python中使用正则表达式搜索三个不同的词

[英]searching three different words using regex in python

I am trying to search three different words in the below output 我正在尝试在以下输出中搜索三个不同的词

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

They are rad_11 , 127.0.0.1 and testing123 . 他们是rad_11127.0.0.1testing123 Can someone help me out ? 有人可以帮我吗 ?

I have tried re.search ('rad_11' '127.0.0.1' 'testing123', output) . 我已经尝试过re.search ('rad_11' '127.0.0.1' 'testing123', output)

You can clear all unnecessary symbols and parse the string: 您可以清除所有不必要的符号并解析字符串:

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'}

For matching any of the patterns, you can use re.findall() : 要匹配任何模式,可以使用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']

Searching all patterns is much simpler: 搜索所有模式要简单得多:

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

From re.findall() 's documentation ; 来自re.findall()文档

Return all non-overlapping matches of pattern in string, as a list of strings. 返回字符串中模式的所有非重叠匹配项,作为字符串列表。 The string is scanned left-to-right, and matches are returned in the order found. 从左到右扫描该字符串,并以找到的顺序返回匹配项。 If one or more groups are present in the pattern, return a list of groups; 如果该模式中存在一个或多个组,则返回一个组列表;否则,返回一个列表。 this will be a list of tuples if the pattern has more than one group. 如果模式包含多个组,则这将是一个元组列表。 Empty matches are included in the result unless they touch the beginning of another match. 空匹配项将包括在结果中,除非它们碰到另一个匹配项的开头。

You could always use : 您可以随时使用:

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

It isn't fancy but it is clear and does the job 它不花哨,但很清楚,可以完成工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM