I have the following code that can replace the email address in a String in Java:
addressStr.replaceFirst("([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})", "")
So, a string with John Smith <john@smith.com> would become John Smith <>. How do I negate it so that it will instead replace all that doesn’t match the email address and have the final result as just john@smith.com?
I tried to put in the ^ and ?<= at the front but it doesn’t work.
Well, it’s not the regex you need to change but the calling code. Your regex matches the e-mail address (in a weird way), and the
replace()removes it from the string.So just use