user user - 2 years ago 94
Ruby Question

Remove phone number from text

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

For example I have:

(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
, but it removes all similar numbers.

Answer Source

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:


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

  • \([0-9]*\) need to capture numbers inside an parentheses(...), but as parentheses is special characters in regex so adding \ before it, [0-9] mean need a number and as its not only 1 number inside so adding * mean 0 or more number should be inside,

  • \s need a space after it,

  • (-|\s) need dash(-) (OR |) space(\s)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download