1

I working on a Regex that'll match both the following pieces of syntax...

1.

aaa accounting system default  
 action-type start-stop  
 group tacacs+

2.

aaa accounting system default start-stop group tacacs+

The best I've got so far is...

^aaa accounting system default (\n action-type |)start-stop(\n |) group tacacs\+

The above Regex will match syntax number 2 but not 1? Pulling my hair out! (I know it's probably simple but I'm a Regex newbie) Any ideas? There are spaces at the beginning of lines 2 & 3 in syntax piece number 1 but aren't being displayed to get a real look at how the syntax is presented take a look at the below Regex101 link. Thanks!

Here it is in Regex101...

https://regex101.com/r/lW8hT1/1

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
yermander
  • 67
  • 7
  • I edited the question so that the sample strings looked the same as at regex101.com. If the spaces can really be any, and there can be any amount of them, I would vote for my answer :) – Wiktor Stribiżew Jan 29 '16 at 11:35

3 Answers3

3

It doesn't work because you have redundant spaces in your optional groups:

^aaa accounting system default(\n action-type|) start-stop(\n|) group tacacs\+

You can write it in a better way using non-capturing groups (?:...) and the optional quantifier ?:

^aaa accounting system default(?:\n action-type)? start-stop\n? group tacacs\+

(in this way you avoid useless captures)

Casimir et Hippolyte
  • 83,228
  • 5
  • 85
  • 113
2

To match across multiple line you will need DOTALL flag:

/(?s)\baaa accounting system default.*?group tacacs\+/

Or else:

/\baaa accounting system default.*?group tacacs\+/s

RegEx Demo

anubhava
  • 664,788
  • 59
  • 469
  • 547
2

You can replace the regular spaces in your pattern with \s that matches any whitespace:

'~^aaa\s+accounting\s+system\s+default(?:\s+action-type)?\s+start-stop\s+group\s+tacacs\+~m'

See the regex demo

Also, I made some other optimizations so that your two types of strings could be matched:

  • ^ - matches start of a line (due to /m) modifier
  • aaa\s+accounting\s+system\s+default - matches a sequence aaa accounting system default where \s+ matches one or more whitespaces
  • (?:\s+action-type)? - an optional action-type (with one or more whitespace before action-type)
  • \s+start-stop\s+group\s+tacacs\+ - matches start-stop group tacacs+ that have 1 or more spaces in between the words.
Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
  • Note that to match all Unicode whitespace, do not forget to add a `/u` modifier to the end of the regex: `'~^aaa\s+accounting\s+system\s+default(?:\s+action-type)?\s+start-stop\s+group\s+tacacs\+~mu'` – Wiktor Stribiżew Jan 29 '16 at 11:36
  • 1
    Great stuff and I learned something too! Thanks for your help! Really appreciate it. – yermander Jan 29 '16 at 11:52
  • You are welcome. Note I also prefer [*non-capturing groups*](http://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group) with `?` quantifier applied to them (like `(?:...)?`) when I need to use an optional character sequence in my pattern (as Casimir notes in his answer). – Wiktor Stribiżew Jan 29 '16 at 11:53
  • Thanks again that non-capturing groups link helped me get a good handle on it – yermander Jan 29 '16 at 12:24