Link to home
Start Free TrialLog in
Avatar of S_B
S_B

asked on

Replace . by [dot] in emails which are placed within text via Regular Expression

In Java i want to replace the '.' in an email which is placed within a text with [dot] and i want to do this via a regular expression just by string.replaceAll("regex", "[dot]")

the tricky part is that the emailaddress ist placed within other (html)text and the dots not belonging to an email must be preserved.

exampletext::
any text. <anytag>...<anytag>
firstname.lastname@domain.com
more text, more tags etc.

After parsing the text should look like this:

any text. <anytag>...<anytag>
firstname[dot]lastname@domain[dot]com
more text, more tags etc.


Any help appreciated!
ASKER CERTIFIED SOLUTION
Avatar of amit_g
amit_g
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
S_B, what can be construed a valid e-mail address is going to be the issue with constructing a pattern to replace periods with [dot], consider these valid e-mail addresses:
a.b.c@d.e.f.g
a@b.c.d

If this is an effort to fool SPAM e-mail address harvesting bots, they've figured out the [dot] trick and it won't fool them.  But, if you must do this, I would first start with a pattern that would match most valid SMTP e-mail address formats, such as:
http://regexlib.com/REDetails.aspx?regexp_id=1336

Then loop through the matches, using a simple replace "." with "[dot]" and "@" with "[at]" if desired.
Like this:


<script>

var myHtml = "<some>Text and Email: adr0.adr1.adr2@ee.com <br>\n more.test: adr0.adr1.adr2@ee.com <br> more.test.";

var dotHtml =  myHtml.replace(/([\w\-]+\.)*\w+\@([\w\-]+\.)*\w+/g,function(m){return m.replace(/\./g,"[dot]")});

alert(dotHtml);
</script>



Or even simplified to:

<script>

var myHtml = "<some>Text and Email: adr0.adr1.adr2@ee.com <br>\n more.test: adr0.adr1.adr2@ee.com <br> more.test.";

var dotHtml =  myHtml.replace(/[\w\-\.]+\@[\w\-\.]+/g,function(m){return m.replace(/\./g,"[dot]")});

alert(dotHtml);
</script>

Avatar of S_B
S_B

ASKER

@amit_g: worked for me!

@Zvonko: Your solution is JavaScript and i need Java...