Search This Blog

Wednesday 22 November 2017

Python - Regular Expressions

Regular expressions in Python let's you find and store values for re-use in your script.

Let's take the following example:
Cisco IOS Software, C880 Software (C880DATA-UNIVERSALK9-M), Version 15.4(2)T1, RELEASE SOFTWARE (fc3)


To extract the software version we can use the following:

re.search(r"^Cisco (.*), Version (\S+),.*$", line).group(2)


^ - beginning of the line
.* - any number of characters
() - store the value inside parentheses
\S - any non-white space characters
+ - any number of previous character
$ - end of the line
group(2) - since we have 2 parenthesis, we extract the value of the second set of parenthesis.
group(0) - matches the entire value of the string

os_version = re.search(r"^Cisco (.*), Version (\S+),.*$", line).group(2)
os_version
15.4(2)T1


To assign an identifier use ?P<name>

os_version = re.search(r"^Cisco (.*), Version (?P<serial_num>\S+),.*$", line).group(2)

match = os_version = re.search(r"^Cisco (.*), Version (?P<serial_num>\S+),.*$", line)
match.groupdict()
{'serial_num'': '15.4(2)T1'}


To make the .* be less greedy we can use ? to capture the least amount.

For a large string with multiple lines, in order to run Regex on a line per line basis we can use the flags option:
re.search(r"^Processor board ID (.*)$", output, flags=re.M)


To capture beyond the first line using .* we can use the re.DOTALL flag. Flag re.I ignores capitalization.
re.search(r"^Cisco.*", output, flags=re.M).group(0)


No comments:

Post a Comment

Nornir Compliance Check