Grep with regular expressions

I am trying to use regex with grep command for Linux

(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$)) 

When I try to use https://www.regextester.com with the contents of the file, I get the desired result, i.e. required fields match but when i try to use it as

 grep '(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))' file1 

all he gives me is null!

What is the problem?

+7
source share
3 answers
 pcregrep -M '(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))' 

did the trick :)

+2
source

I do not think grep understands character classes like \w and \s . Try using grep -E or egrep . ( grep -E equivalent to egrep , egrep just shorter for input.)

So your command will be:

 egrep '(^\s*\*\s*\[ \][^\*]+?(\w*\:[^\*]+\d$)|([^\*]+[.]com[.]au$))' file1 
+3
source

grep(1) uses POSIX Basic Regular Expressions by default and POSIX Extended Regular Expressions when used with the -E option.

In POSIX regular expressions, non-special characters have undefined behavior when escaping, for example. \s , and there is no syntax for non-greedy matching, for example. +? . In addition, the BRE operators + and | not available, and brackets must be escaped to perform grouping.

the POSIX character classes [[:space:]] and [[:alnum:]_] are portable alternatives to \s and \w respectively.

Excluding the next matching character from a repetition can be used to emulate an undesired match, for example. [^*]+?\w*: equivalent to [^*[:alnum:]_:]+[[:alnum:]_]*:

This regular expression can be represented as multiple BREs:

 grep -e '^[[:space:]]*\*[[:space:]]\{1,\}\[ \][^*[:alnum:]_+]\{1,\}[[:alnum:]_]*:[^*]\{1,\}[[:digit:]]$' \ -e '[^*]\{1,\}\.com\.au$' file1 

or ERE:

 grep -E '^[[:space:]]*\*[[:space:]]*\[ \][^*[:alnum:]_:]+[[:alnum:]_]*:[^*]+[[:digit:]]$|[^*]+\.com\.au$' \ file1 

Note that the GNU grep(1) implementation allows for both short character classes ( \s and \w ) and unwanted repetition ( +? ) As non-portable extensions.

0
source

All Articles