Which option would you choose to force grep to use a basic regular expression (BRE)

Much of what we think of as "regular expressions" are actually called Extended Regular Expressions (or EREs) in POSIX. But your grep call appears to be in Basic Regular Expression mode (or BRE, for short). BREs and EREs have a number of differences. One of these differences is that you have to escape metacharacters: + is a literal plus sign unless you escape it with a backslash. Your grep command appears to be in BRE mode, so try using \+ instead of a plain +.

If you're being really strict about BRE versus ERE, then BREs don't actually support the + and ? operations, though you can simulate them with \{1,\} and \{0,1\} constructs, respectively. Strict BREs don't support the | operator either, and I'm not sure how you simulate that in a BRE. However, unlike EREs, BREs support backreferences (which look a lot like they do in Perl, except that you have to escape the parens).

Some grep implementations (like GNU) support the ?, +, and | operators in BRE mode, though you have to escape them like any other metacharacters: \?, \+, and \|. But no grep implementations that I'm aware of support backreferencing in ERE mode.

To force your grep to use ERE mode, you can use the -E option to grep, or you can call it as egrep instead.

Prerequisite: grep

Basic Regular Expression

Regular Expression provides an ability to match a “string of text” in a very flexible and concise manner. A “string of text” can be further defined as a single character, word, sentence or particular pattern of characters.

Like the shell’s wild–cards which match similar filenames with a single expression, grep uses an expression of a different sort to match a group of similar patterns.

  • [ ]: Matches any one of a set characters
  • [ ] with hyphen: Matches any one of a range characters
  • ^: The pattern following it must occur at the beginning of each line
  • ^ with [ ] : The pattern must not contain any character in the set specified
  • $: The pattern preceding it must occur at the end of each line
  • . (dot): Matches any one character
  • \ (backslash): Ignores the special meaning of the character following it
  • *: zero or more occurrences of the previous character
  • (dot).*: Nothing or any numbers of characters.

Examples

(a) [ ] : Matches any one of a set characters

  1. $grep  “New[abc]”  filename
    

    It specifies the search pattern as :

    Newa , Newb or Newc
    
  2. $grep  “[aA]g[ar][ar]wal”  filename
    

    It specifies the search pattern as

    Agarwal , Agaawal , Agrawal , Agrrwal
    
    agarwal , agaawal , agrawal , agrrwal
    


(b) Use [ ] with hyphen
: Matches any one of a range characters

  1. $grep  “New[a-e]” filename
    

    It specifies the search pattern as

    Newa , Newb or Newc , Newd, Newe
    
  2. $grep  “New[0-9][a-z]”  filename
    

    It specifies the search pattern as: New followed by a number and then an alphabet.

    New0d, New4f etc
    

(c ) Use ^: The pattern following it must occur at the beginning of each line

  1. $grep  “^san”  filename
    

    Search lines beginning with san. It specifies the search pattern as

    sanjeev ,sanjay, sanrit , sanchit , sandeep etc.
    
  2. $ls –l |grep  “^d” 
    

    Display list of directories only

  3. $ls –l |grep  “^-” 
    

    Display list of regular files only

(d) Use ^ with [ ]: The pattern must not contain any character in the set specified

  1. $grep  “New[^a-c]”  filename
    

    It specifies the pattern containing the word “New” followed by any character other than an ‘a’,’b’, or ‘c’

  2. $grep  “^[^a-z A-Z]”  filename
    

    Search lines beginning with an non-alphabetic character

(e) Use $: The pattern preceding it must occur at the end of each line

$ grep "vedik$" file.txt

(f) Use . (dot): Matches any one character

$ grep "..vik" file.txt
$ grep "7..9$" file.txt

(g) Use \ (backslash): Ignores the special meaning of the character following it

  1. $ grep "New\.\[abc\]" file.txt

    It specifies the search pattern as New.[abc]

  2. $ grep "S\.K\.Kumar" file.txt
    

    It specifies the search pattern as

    S.K.Kumar
    

(h) Use *: zero or more occurrences of the previous character

$ grep "[aA]gg*[ar][ar]wal" file.txt


(i) Use (dot).*
: Nothing or any numbers of characters.

$ grep "S.*Kumar" file.txt

This article is contributed by Akshay Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Which option would you choose to force grep to use a basic regular expression?

Grep Regular Expression In its simplest form, when no regular expression type is given, grep interpret search patterns as basic regular expressions. To interpret the pattern as an extended regular expression, use the -E ( or --extended-regexp ) option.

What is regular expression in grep command?

Regular expressions consist of letters and numbers, in addition to characters with special meaning to grep . These special characters, called metacharacters, also have special meaning to the system.

Does grep support regular expressions?

The grep understands three different types of regular expression syntax as follows: basic (BRE) extended (ERE) perl (PCRE)

Which search command allows for the use of regular expressions?

The grep (Global Regular Expression Print) is a unix command utility that can be used to find specific patterns described in “regular expressions”, a notation which we will learn shortly. For example, the “grep” command can be used to match all lines containing a specific pattern.