This page was exported from phaq
[ http://phaq.phunsites.net ] Export date: Sat Jun 10 2:12:23 2023 / +0000 GMT |
A project I'm currently working on involges much Regurlar Expressions trickery to parse values from Cisco's running configuration. Here's how to extract a complete interface block not in 'shutdown' state. Imagine a Cisco configuration block like this:
And here the same again without the 'stutdown' statement:
Thinking about a case where you want to extract just these blocks from multiple config files, but only if they don't contain the 'shutdown' statement. The solution to this with Regular Expressions is called negative look-ahead, so to speak "match something not followed by something else". The trick is to make anchored matches within the context we're searching. Looking at the block, we get multiple anchors:
So an extended expression, which would match just this criteria looks like this: /^interface .*[rn]+(?:^s.*[rn])+(?:^![rn]+)+/m This would match any interface configuration block. Now if we want only the interface in 'no shutdown' state, we add a negative look-ahead which can be spoken of as this:
Thus, our expression now looks like this: /^interface .*[rn]+(?:^s(?!shutdown).*[rn])+(?:^![rn]+)+/m This will return just the matches for interfaces in 'no shutdown' state. |
Powered by [ Universal Post Manager ] plugin. HTML saving format developed by gVectors Team www.gVectors.com |