PCRE Regex to SED

Want PCRE (Perl Compatible Regular Expressions)? Why don't you use perl instead?

perl -pe 's/[a-zA-Z0-9]+[@][a-zA-Z0-9]+[\.][A-Za-z]{2,4}/[emailaddr]/g' \
    <<< "My email is [email protected]"

Output:

My email is [emailaddr]

Write output to a file with tee:

perl -pe 's/[a-zA-Z0-9]+[@][a-zA-Z0-9]+[\.][A-Za-z]{2,4}/[emailaddr]/g' \
    <<< "My email is [email protected]" | tee /path/to/file.txt > /dev/null

Use the -r flag enabling the use of extended regular expressions. ( -E instead of -r on OS X )

echo "My email is [email protected]" | sed -r 's/[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[A-Za-z]{2,4}/[emailaddr]/g'

Ideone Demo


for multiline use the 0! perl -0pe 's/search/replace/gms' file


GNU sed uses basic regular expressions or, with the -r flag, extended regular expressions.

Your regex as a POSIX basic regex (thanks mklement0):

[[:alnum:]]\{1,\}@[[:alnum:]]\{1,\}\.[[:alpha:]]\{2,4\}

Note that this expression will not match all email addresses (not by a long shot).

Tags:

Regex

Sed

Pcre