9 Replies - 588 Views - Last Post: 10 October 2017 - 10:51 AM Rate Topic: -----

#1 foodandcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-March 16

Can I do this with regex?

Posted 08 October 2017 - 09:13 PM

Hi all,
I need to assert the condition that every time there is a GigabitEthernet interface with a description name of NNCX or ATN, it should be followed by this command: isis ldp-sync


Sample haystack:
#
interface GigabitEthernet1/2/0.2277
description 2400-NNCX8H
isis ldp-sync
#
interface GigabitEthernet1/2/2
negotiation auto
undo shutdown
#
interface GigabitEthernet1/2/2.200
description 2437-ATN91
isis ldp-sync
#
interface GigabitEthernet1/2/5.3660
vlan-type dot1q 3660
description NERH4
#
interface GigabitEthernet1/2/2.200
description 2437-ATN91
isis cost 1000
isis small-hello
isis ldp-sync
#


https://regex101.com/r/3ccUaJ/2

Is this case possible with regex and if so how?

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Can I do this with regex?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5074
  • View blog
  • Posts: 13,697
  • Joined: 18-April 07

Re: Can I do this with regex?

Posted 08 October 2017 - 10:27 PM

What is wrong with the regex you posted? Looks like it is matching 4 instances as it should given what you have defined.
Was This Post Helpful? 0
  • +
  • -

#3 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 540
  • Joined: 19-May 14

Re: Can I do this with regex?

Posted 08 October 2017 - 11:48 PM

Yes this is possible. You just need to adjust the logic a little bit.

So here's the regex I use to detect if there is a GigabitEthernet Interface with the name NNCX or ATN

description [0-9]{4}-(ATN|NNCX)[0-9A-Z]*


Once I get a message from the console I will check it against the regex
If the regex doesn't match I'll return false otherwise I'll send the command to the console.
Was This Post Helpful? 0
  • +
  • -

#4 foodandcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-March 16

Re: Can I do this with regex?

Posted 09 October 2017 - 12:04 AM

View PostMartyr2, on 08 October 2017 - 10:27 PM, said:

What is wrong with the regex you posted? Looks like it is matching 4 instances as it should given what you have defined.

The problem with my regex is there is no way for me to assert if all GE interfaces have that commmand. Need to add a conditional if statement so that if a GE with those two names in description.exist it must have that command, but I'm not sure how.

Thanks
Was This Post Helpful? 0
  • +
  • -

#5 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 540
  • Joined: 19-May 14

Re: Can I do this with regex?

Posted 09 October 2017 - 04:53 AM

View Postfoodandcode, on 09 October 2017 - 12:04 AM, said:

View PostMartyr2, on 08 October 2017 - 10:27 PM, said:

What is wrong with the regex you posted? Looks like it is matching 4 instances as it should given what you have defined.

The problem with my regex is there is no way for me to assert if all GE interfaces have that commmand. Need to add a conditional if statement so that if a GE with those two names in description.exist it must have that command, but I'm not sure how.

Thanks


What about my example? Is it what you are looking for?
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: Can I do this with regex?

Posted 09 October 2017 - 06:55 AM

Since this is posted in PHP I thought it would be good to do it in PHP:
<?php
$test = <<<here
interface GigabitEthernet1/2/0.2277 description 2400-NNCX8H isis ldp-sync
#
interface GigabitEthernet1/2/2
 negotiation auto
 undo shutdown
#
interface GigabitEthernet1/2/2.200
 description 2437-ATN91
 isis ldp-sync
#
interface GigabitEthernet1/2/5.3660
 vlan-type dot1q 3660
 description NERH4
#
interface GigabitEthernet1/2/2.200
 description 2437-ATN91
 isis cost 1000
 isis small-hello
 isis ldp-sync
#
interface GigabitEthernet 1/2/0.2288
 description 2400-NNCX8H
 isis ldp-sync
#
interface MegabitEthernet 1/2/0.2288
 description 2400-NNCX8H
 isis ldp-sync
here;

$interfaces = explode("#", $test);
checkGigBit($test);

function checkGigBit($testCases) {
    $regExp = '/(.?GigabitEthernet)(.*?description [0-9]{4}-[NNCX|ACN])(.*isis ldp-sync)/';
    $Cases = explode("#", $testCases);

    foreach($Cases as $case) {
        $temp = str_replace("\n", "", $case); // Remove line breaks.
        preg_match($regExp, $temp, $matches);
        var_dump($matches);
        assert(count($matches)== 4);
    }
}


This is close, but not the solution since it will fail if the interface is not "GigabitEthernet". You'll probably need to use regex conditionals to fix it.
Was This Post Helpful? 0
  • +
  • -

#7 foodandcode  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-March 16

Re: Can I do this with regex?

Posted 09 October 2017 - 08:14 PM

View PostRamonRobben, on 09 October 2017 - 04:53 AM, said:

View Postfoodandcode, on 09 October 2017 - 12:04 AM, said:

View PostMartyr2, on 08 October 2017 - 10:27 PM, said:

What is wrong with the regex you posted? Looks like it is matching 4 instances as it should given what you have defined.

The problem with my regex is there is no way for me to assert if all GE interfaces have that commmand. Need to add a conditional if statement so that if a GE with those two names in description.exist it must have that command, but I'm not sure how.

Thanks


What about my example? Is it what you are looking for?

The regex you posted works the same as my own regex, however Im not sure what you mean by checking the console for the return value.
Im wondering if this is possible to do using one simple regex.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: Can I do this with regex?

Posted 10 October 2017 - 04:05 AM

View Postfoodandcode, on 09 October 2017 - 10:14 PM, said:

Im wondering if this is possible to do using one simple regex.

Yes and no. Regex expects it all to be on one line, so you need to strip the line feeds first. Then it's a matter of determining if these three conditions are met:
  • Does it contain "GigabitEthernet"?
  • Does it contain "description " followed by a four digit number, a dash, and either "NNCX" or "ACN"?
  • Does it contain "isis ldp-sync"?

If it does, we have it return an array of results, otherwise it returns an empty array. Hence the conditional.
This code
<?php
$test = <<<here
interface GigabitEthernet1/2/0.2277 description 2400-NNCX8H isis ldp-sync
#
interface GigabitEthernet1/2/2
 negotiation auto
 undo shutdown
#
interface GigabitEthernet1/2/2.200
 description 2437-ATN91
 isis ldp-sync
#
interface GigabitEthernet1/2/5.3660
 vlan-type dot1q 3660
 description NERH4
#
interface GigabitEthernet1/2/2.200
 description 2437-ATN91
 isis cost 1000
 isis small-hello
 isis ldp-sync
#
interface GigabitEthernet 1/2/0.2288
 description 2400-NNCX8H
 isis ldp-sync
#
interface MegabitEthernet 1/2/0.2288
 description 2400-NNCX8H
 isis ldp-sync
here;

$interfaces = explode("#", $test);
checkGigBit($test);

function checkGigBit($testCases) {
    $regExp = '/(GigabitEthernet)(.*?description [0-9]{4}-[NNCX|ACN])(.*isis ldp-sync)|(.*?fail)/';
    $Cases = explode("#", $testCases);

    foreach($Cases as $case) {
        $temp = str_replace("\n", "", $case);
        echo $case."<br>";
        preg_match($regExp, $temp, $matches);
        echo "Matches ";
        var_dump($matches);
//        assert(count($matches)== 4);
    }
}


produces this output for me.
interface GigabitEthernet1/2/0.2277 description 2400-NNCX8H isis ldp-sync 
Matches
array (size=4)
  0 => string 'GigabitEthernet1/2/0.2277 description 2400-NNCX8H isis ldp-sync' (length=63)
  1 => string 'GigabitEthernet' (length=15)
  2 => string '1/2/0.2277 description 2400-N' (length=29)
  3 => string 'NCX8H isis ldp-sync' (length=19)
interface GigabitEthernet1/2/2 negotiation auto undo shutdown 
Matches
array (size=0)
  empty
interface GigabitEthernet1/2/2.200 description 2437-ATN91 isis ldp-sync 
Matches
array (size=4)
  0 => string 'GigabitEthernet1/2/2.200 description 2437-ATN91 isis ldp-sync' (length=61)
  1 => string 'GigabitEthernet' (length=15)
  2 => string '1/2/2.200 description 2437-A' (length=28)
  3 => string 'TN91 isis ldp-sync' (length=18)
interface GigabitEthernet1/2/5.3660 vlan-type dot1q 3660 description NERH4 
Matches
array (size=0)
  empty
interface GigabitEthernet1/2/2.200 description 2437-ATN91 isis cost 1000 isis small-hello isis ldp-sync 
Matches
array (size=4)
  0 => string 'GigabitEthernet1/2/2.200 description 2437-ATN91 isis cost 1000 isis small-hello isis ldp-sync' (length=93)
  1 => string 'GigabitEthernet' (length=15)
  2 => string '1/2/2.200 description 2437-A' (length=28)
  3 => string 'TN91 isis cost 1000 isis small-hello isis ldp-sync' (length=50)
interface GigabitEthernet 1/2/0.2288 description 2400-NNCX8H isis ldp-sync 
Matches
array (size=4)
  0 => string 'GigabitEthernet 1/2/0.2288 description 2400-NNCX8H isis ldp-sync' (length=64)
  1 => string 'GigabitEthernet' (length=15)
  2 => string ' 1/2/0.2288 description 2400-N' (length=30)
  3 => string 'NCX8H isis ldp-sync' (length=19)
interface MegabitEthernet 1/2/0.2288 description 2400-NNCX8H isis ldp-sync
Matches
array (size=0)
  empty

Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: Can I do this with regex?

Posted 10 October 2017 - 06:06 AM

This line in my code: $regExp = '/(GigabitEthernet)(.*?description [0-9]{4}-[NNCX|ACN])(.*isis ldp-sync)|(.*?fail)/'; would be better as $regExp = '/(GigabitEthernet)(.*?description [0-9]{4}-[NNCX|ACN])(.*isis ldp-sync)|(.*?: ...)/';
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3715
  • Posts: 13,474
  • Joined: 08-August 08

Re: Can I do this with regex?

Posted 10 October 2017 - 10:51 AM

This is bugging me! I have something that almost works, but it appears that the "else" part of the regex is being treated as an "or"
<?php
$test = <<<here
interface GigabitEthernet1/2/2
 negotiation auto
 undo shutdown
#
interface GigabitEthernet1/2/2
 description 2400-SomethingElse
 undo shutdown
#
interface GigabitEthernet1/2/0.2277
 description 2400-NNCX8H
 isis ldp-sync
#
interface GigabitEthernet1/2/2.200
 description 2437-ATN91
 isis ldp-sync
#
interface GigabitEthernet1/2/5.3660
 vlan-type dot1q 3660
 description NERH4
#
interface GigabitEthernet1/2/2.200
 description 2437-ATN91
 isis cost 1000
 isis small-hello
 isis ldp-sync
#
interface GigabitEthernet 1/2/0.2288
 description 2400-NNCX8H
 isis ldp-sync
#
interface MegabitEthernet 1/2/0.2288
 description 2400-ABCX8H
 isAs abc-mesh
here;

$interfaces = explode("#", $test);
checkGigBit($test);

function checkGigBit($testCases) {
    $regExp = '/(?(?=interface\ GigabitEthernet)(description [0-9]{4}-[NNCX|ACN])(ldp-sync)|(.?descr))/s';
    $Cases = explode("#", $testCases);

    foreach($Cases as $case) {
        $temp = str_replace("\n", "<br>", $case);
        echo $temp;
        preg_match($regExp, $case, $matches);
        echo "Matches ".count($matches)."<br>";
        var_dump($matches);
    }
}



output:
interface GigabitEthernet1/2/2
negotiation auto
undo shutdown
Matches 0
array (size=0)
  empty

interface GigabitEthernet1/2/2
description 2400-SomethingElse
undo shutdown
Matches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

interface GigabitEthernet1/2/0.2277
description 2400-NNCX8H
isis ldp-sync
Matches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

interface GigabitEthernet1/2/2.200
description 2437-ATN91
isis ldp-sync
Matches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

interface GigabitEthernet1/2/5.3660
vlan-type dot1q 3660
description NERH4
Matches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

interface GigabitEthernet1/2/2.200
description 2437-ATN91
isis cost 1000
isis small-hello
isis ldp-sync
Matches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

interface GigabitEthernet 1/2/0.2288
description 2400-NNCX8H
isis ldp-sync
Matches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

interface MegabitEthernet 1/2/0.2288
description 2400-ABCX8H
isAs abc-meshMatches 4
array (size=4)
  0 => string ' descr' (length=6)
  1 => string '' (length=0)
  2 => string '' (length=0)
  3 => string ' descr' (length=6)

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1