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
. Can someone help me out ?
I have tried 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()
:
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 ;
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.