/ #pattern delimiter
^ #start of string
(.) #capture group #1 containing the first character
(.*?)#capture group #2 containing zero or more characters (lazy, aka non-greedy)
([^@]?)#capture group #3 containing an optional single non-@ character
(?=@[^@]+$)#require that the next character is @ then one or more @ until the end of the string
/ #pattern delimiter
u #unicode/multibyte pattern modifier
<?php$string='abcedf@gmail.com';
preg_match('/^.\K[a-zA-Z\.0-9]+(?=.@)/',$string,$matches);//here we are gathering this part bced$replacement= implode("",array_fill(0,strlen($matches[0]),"*"));//creating no. of *'sechopreg_replace('/^(.)'.preg_quote($matches[0])."/", '$1'.$replacement, $string);