Home > Software design >  regex compatibility with grep
regex compatibility with grep

Time:11-08

I have this regex that works as expected.

due (?:\w  ){3}community

As seen here...

https://regex101.com/r/8LDW9v/35

and the sample text is:

unfortunately due to the current community size I am not comfortable granting you permanent permissions yet, but I have granted it for 6 months given your experience (normally we start at 3).  vote for that. due payment2342 of bill community should be made. and then due to some un community events again due one to three community

But this grep command does not return anything...

grep -E "(due (?:\w  ){3}community)" mytext.txt

CodePudding user response:

Use -P (Perl regular expression) flag:

$ grep -oP "due (?:\w  ){3}community" test.txt
due to the current community
due payment2342 of bill community
due to some un community
due one to three community

I'm not sure that ?: is supported in extended regex. If you want to use -E, you can change your regex to:

$ grep -oE "(due (\w  ){3}community)" test.txt
due to the current community
due payment2342 of bill community
due to some un community
due one to three community

This will however capture the third word of each match in a group.

CodePudding user response:

Grep does not support non capture groups, so you can change the non capture group into a capture group and omit the outer parenthesis of the pattern as that group has no purpose in the pattern.

Use -o to print only the matching parts.

grep -Eo "due (\w  ){3}community" mytext.txt
  • Related