• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 363
  • Last Modified:

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!
0
S_B
Asked:
S_B
1 Solution
 
amit_gCommented:
Try something like this ...

string.replaceAll("(.+)\.(.+)@(.+)\.(.+)", "$1[dot]$2@$3[dot]$4")
0
 
ddrudikCommented:
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.
0
 
ZvonkoSystems architectCommented:
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>



0
 
ZvonkoSystems architectCommented:
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>

0
 
S_BAuthor Commented:
@amit_g: worked for me!

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now