Is it possible to replace half or x characters of a matched group?

I have had a request for a partial email capturing, so something like [email protected] becomes ***mple123@***def.com

I can do this if the characters before and after the @ are 3 characters long,

([^|@]{0,3})([^]{0,3})

This captures [email protected] perfectly and I can substitute for ***@***.com but if it's over 3 characters long on each end, for instance [email protected] it becomes ******[email protected]

The other way I can see is capture everything until the @ and everything to the . but then this won't be a partial capture. Is this possible?

9

1 Answer

You can use

sed -E 's/^[^@]{0,3}|(@)[^.]{0,3}/\1***/g' 

Details

  • -E - enables the POSIX ERE syntax that does not require too much escaping here
  • ^[^@]{0,3} - zero to three occurrences of any char other than a @ at the start of the string
  • | - or
  • (@) - Group 1: a @ char
  • [^.]{0,3} - zero to three occurrences of any char other than a .
  • \1*** replaces with Group 1 value + ***.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.