4

I need to implement the same functionality in my Rails app as AirBnB has in their iOS app. When you write to a person on AirBnB, your phone, email and address will be blocked, so you get something that looks like this:

Input:

Email addresses likeemail@example.com or email AT example DOT com should be replaced. Phone numbers like 347 323 4567 or tree four seven, three two three four five six seven should also be replace.

Output:

Email addresses like (email hidden) or (email hidden) should be replaced. Phone numbers like (phone hidden) or (phone hidden) should also be replace.

This is to ensure that people can't make "offline" deals. I've looked everywhere, but it doesn't seem like there is any gems our there that have done something similar.

This seems like one of those things with a thousand edge cases, so would rather not implement it myself. Does anyone know of any gems already out there?

Holger Sindbaek
  • 2,278
  • 6
  • 41
  • 68

1 Answers1

0

The following (taken from this question) will have some false negatives (using regular expressions for email addresses always will) but will catch the majority of emails in a string

'email addresses like me@example.com'.gsub(
  /^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, '(email hidden)')
Community
  • 1
  • 1
glittershark
  • 534
  • 2
  • 6
  • 19
  • 1
    So that takes care of emails in non "cheating" form. If someone where to write test at gmail dot com, it wouldn't catch it though. Same with phones and addresses. Any other suggestions? – Holger Sindbaek Apr 14 '15 at 15:54
  • I mean, `/\b.+?\bat.+?\bdot.+?\b/` would work for that – glittershark Apr 14 '15 at 16:15
  • Thanks for the comment. Thing I'm looking to hear, is if there's anyone else that have already done something like this for phone, email and addresses. It seems like one of those things with a thousand edge cases, so I'd rather not do it myself. – Holger Sindbaek Apr 14 '15 at 16:17
  • @HolgerEdwardWardlowSindbæk `/(\d{3}\s?\d{3}\s?\d{4}|one|two|three|four|five|six|seven|eight|nine)/` should work for your phone number situation. Also if your question is can someone do this for me or give me a resource that already has then your question is off topic. – engineersmnky Apr 14 '15 at 16:26
  • @engineersmnky Why in the world would it be off topic? I don't want anyone to do this for me. I wanted to hear if there was someone who has encountered this problem with all its many edge cases has spent time making it already, so I don't have to do it myself. Let me know if you know of any libraries. – Holger Sindbaek Apr 14 '15 at 18:38
  • @HolgerEdwardWardlowSindbæk it is the last statement in you comment that makes it off topic for StackOverflow. **Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam.** [Taken from](http://stackoverflow.com/help/on-topic). That being said email regex is always very difficult and the fact that you are asking for more than just that will only make it more so. I wish you the best of luck. – engineersmnky Apr 14 '15 at 18:42