JunOS Complex Regular Expressions – Understanding Juniper Regex

juniperjuniper-junos

I saw this explanation in the JNCIS study guide book ch1 (community regex examples) and i'm a bit confused:

^65000:.{2,3}$ – AS number is 65000, community value is any 2 or 3 digit number ex. 65000:123, 65000:16, and 65000:999

^65010:45.{2}9$ – AS number is 65010. The community value is a five-digit number that begins with 45 and ends with 9. The third and fourth digits are any single number repeated twice ex. 65010:45119, 65010:45999, and 65010:45339.

If .{2} means any single number repeated twice, then i'd have argued .{2,3} if any single number repeated at least twice and at most thrice and not any 2 or 3 digit number as explained in the book.

What is the right interpretation of these 2 regex ?

Thanks,

Best Answer

I set up a quick'n'dirty lab and it would appear that the first example is actually correct, rather than the second.

The first example, I've got three routes being received, each tagged with a community of 65000:123, 65000:999 or 65000:16:

{primary:node0}
root@lab-srx240h> show route table CE3.inet.0 community ^65000:.{2,3}$ detail

CE3.inet.0: 7 destinations, 7 routes (7 active, 0 holddown, 0 hidden)
3.3.3.3/32 (1 entry, 1 announced)
        *BGP    Preference: 170/-101
                Next hop type: Router, Next hop index: 1383
                Address: 0x15d464c
                Next-hop reference count: 8
                Source: 192.168.88.3
                Next hop: 192.168.88.3 via reth4.100, selected
                State: <Active Ext>
                Peer AS: 65505
                Age: 7:34
                Task: BGP_65505.192.168.88.3+179
                Announcement bits (1): 0-KRT
                AS path: 65505 65505 I
                Communities: 65000:123 target:65505:1
                Accepted
                Localpref: 100
                Router ID: 192.168.6.1

192.168.5.0/24 (1 entry, 1 announced)
        *BGP    Preference: 170/-101
                Next hop type: Router, Next hop index: 1383
                Address: 0x15d464c
                Next-hop reference count: 8
                Source: 192.168.88.3
                Next hop: 192.168.88.3 via reth4.100, selected
                State: <Active Ext>
                Peer AS: 65505
                Age: 7:34
                Task: BGP_65505.192.168.88.3+179
                Announcement bits (1): 0-KRT
                AS path: 65505 I
                Communities: 65000:999 target:65505:1
                Accepted
                Localpref: 100
                Router ID: 192.168.6.1

192.168.88.0/31 (1 entry, 1 announced)
        *BGP    Preference: 170/-101
                Next hop type: Router, Next hop index: 1383
                Address: 0x15d464c
                Next-hop reference count: 8
                Source: 192.168.88.3
                Next hop: 192.168.88.3 via reth4.100, selected
                State: <Active Ext>
                Peer AS: 65505
                Age: 7:34
                Task: BGP_65505.192.168.88.3+179
                Announcement bits (1): 0-KRT
                AS path: 65505 I
                Communities: 65000:16 target:65505:1
                Accepted
                Localpref: 100
                Router ID: 192.168.6.1

The second example I've got a route tagged with 65010:45129 (not 65010:45119 as per the book) and I still get a match:

{primary:node0}
root@lab-srx240h> show route table CE3.inet.0 community ^65010:45.{2}9$ detail
CE3.inet.0: 7 destinations, 7 routes (7 active, 0 holddown, 0 hidden)
1.0.0.10/32 (1 entry, 1 announced)
        *BGP    Preference: 170/-101
                Next hop type: Router, Next hop index: 1383
                Address: 0x15d464c
                Next-hop reference count: 8
                Source: 192.168.88.3
                Next hop: 192.168.88.3 via reth4.100, selected
                State: <Active Ext>
                Peer AS: 65505
                Age: 2:23
                Task: BGP_65505.192.168.88.3+179
                Announcement bits (1): 0-KRT
                AS path: 65505 65505 I
                Communities: 65010:45129 target:65505:1 origin:65505:1
                Accepted
                Localpref: 100
                Router ID: 192.168.6.1

So yes, .{2} means any two characters and .{2,3} means any two-to-three characters.

Related Topic