Learn How We Can Help You Keep Teleworkers Protected During the COVID-19 Crisis

Network Change & Configuration Management

Reply
Highlighted
Accepted Solution

Counting matches of a regex in XML rule

Techie
Posts: 4
2635     0

Is there a way to count the number of matches of a given regex within a NetMRI rule?  For example, say I have the following text I am matching against:

 

ip access-list 72

 10 permit 1.1.1.1/32 2.2.2.2/32

 20 permit 3.3.3.3/32 2.2.2.2/32

 

My regex is /^\s[0-9]+ permit/m

 

Is there a way to count the number of times my regex matches the input (2 in this case)?

Highlighted

Re: Counting matches of a regex in XML rule

Adviser
Posts: 353
2636     0

Yes, I would use a ConfigBlockCheck. Those can be nested too. So, you could do something like:

 

<Expr op='array' output='access-lists'/>
<Expr op='array' output='rules-counts'/> 
<ConfigBlockCheck boundary-method='indent' block-start='^ip access list (.*)'>
  <Assign variable="rule-count" value="0"/>
  <ConfigBlockCheck boundary-method='line-count' block-start='/^\s[0-9]+ permit/m' line-count='1'>
    <!-- any code in here will be executed for each permit line -->
    <Expr output='rule-count' op='+'><Expr variable='rule-count'/><Expr value='1'/></Expr>
  </ConfigBlockCheck>
  <Expr op='push'><Expr variable='access-lists'/><Expr variable='_block_start_match_1'/></Expr>
  <Expr op='push'><Expr variable='rule-counts'/><Expr variable='rule-count'/></Expr>
</ConfigBlockCheck>

<!-- at this point, you have two matching arrays with access list names and permit rule counts -->

This is not tested code, but should give you the basic idea.

 

John

Highlighted

Re: Counting matches of a regex in XML rule

Techie
Posts: 4
2636     0

This worked perfectly!  Thank you!

Highlighted

Re: Counting matches of a regex in XML rule

Adviser
Posts: 422
2636     0

Dan, can you post the example for others to learn Smiley Happy

 

Thanks,

Sif

Follow me on LinkedIn: https://www.linkedin.com/in/sifbaksh
Twitter: https://twitter.com/sifbaksh
Showing results for 
Search instead for 
Do you mean 

Recommended for You