what is the programming language invloved? For most PCRE regex flavors as in Perl, PHP, Javascript, C#, et al., you can use a function call in the replacement part of the "replace" expression/function. i.e., for Perl you can try sth like: $line =~ s{ [^.]*(?:\.[^.]*){2,} }{ s#\.#|#g; $_ }ex; this changes: ww.aaa.com.cn/part1\index.html to ww|aaa|com|cn/part1\index|html but keeps: ww.aaa untouched.. You may want to add boundaries/anchors to your pattern accordingly. Good luck