How can you use extended regular expressions with grep or sed without using -E or -r?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

How can you use extended regular expressions with grep or sed without using -E or -r?

Explanation:
When grep and sed run in basic regular expression mode by default, you can still express extended regex patterns by escaping the metacharacters that implement grouping, alternation, and repetition. By prefixing those characters with a backslash, you activate their extended meaning inside the basic engine. So you would write grouping as \( … \), alternation as \|, and repetition as \{m,n\}. This is why backslashes before those symbols lets you achieve extended regex behavior without using -E or -r. For example, grep '\(cat\|dog\)' file matches either cat or dog. To require a sequence of three a's, use grep 'a\{3\}' file. In sed, you can do something like sed 's/\(foo\|bar\)/baz/g' to replace either foo or bar with baz.

When grep and sed run in basic regular expression mode by default, you can still express extended regex patterns by escaping the metacharacters that implement grouping, alternation, and repetition. By prefixing those characters with a backslash, you activate their extended meaning inside the basic engine. So you would write grouping as ( … ), alternation as |, and repetition as {m,n}. This is why backslashes before those symbols lets you achieve extended regex behavior without using -E or -r.

For example, grep '(cat|dog)' file matches either cat or dog. To require a sequence of three a's, use grep 'a{3}' file. In sed, you can do something like sed 's/(foo|bar)/baz/g' to replace either foo or bar with baz.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy