user user - 7 months ago 39
Ruby Question

Rails remove phone number from text

How can I remove phone numbers from a string if they are in different formats?

For example I have:

text='
(093) 123-34-56 (068) 123 45 67 (095) 123 456 78
Refresh Rate: 60Hz (Native). Backlight: LED (Full Array)
Smart Functionality: Yes - xx TV Streaming Platform
Dimensions (W x H x D): TV without stand (inches) : 28.98x17x3.18, TV with stand (inches) : 28.98x18.68x7.78'


How can I remove these phone numbers?

(093) 123-34-56 (068) 123 45 67 (095) 123 456 78


I have tried
gsub
, but it removes all similar numbers.

Answer

You can use:

text.gsub(/\([0-9]*\)\s[0-9]*(-|\s)[0-9]*(-|\s)[0-9]*/, '')

this one will remove the phones in the format you specified in your text:

  • (XXX) XXX-XX-XX
  • (XXX) XXX XX XX

and always when you are trying to write regex try to use this Rubular

Comments