regexsedpcre

PCRE Regex to SED


I am trying to take PCRE regex and use it in SED, but I'm running into some issues. Please note that this question is representative of a bigger issue (how to convert PCRE regex to work with SED) so the question is not simply about the example below, but about how to use PCRE regex in SED regex as a whole.

This example is extracting an email address from a line, and replacing it with "[emailaddr]".

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

I've tried the following replace regex:

([a-zA-Z0-9]+[@][a-zA-Z0-9]+[\.][A-Za-z]{2,4})
[a-zA-Z0-9]+[@][a-zA-Z0-9]+[\.][A-Za-z]{2,4}
([a-zA-Z0-9]+[@][a-zA-Z0-9]+[.][A-Za-z]{2,4})
[a-zA-Z0-9]+[@][a-zA-Z0-9]+[.][A-Za-z]{2,4}

I've tried changing the delimited of sed from s/find/replace/g to s|find|replace|g as outlined here (stack overflow: pcre regex to sed regex).

I am still not able to figure out how to use PCRE regex in SED, or how to convert PCRE regex to SED. Any help would be great.


Solution

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

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

    Ideone Demo