0

I would like to remove telephone numbers from a string using C#. I have been experimenting using different variations of regex with little success.

I would like a solution that is quick to execute (sub 0.1s if possible) as it will be used extensively in a production environment.

This is the code that I have been testing.

var stringContainingPhoneNumber = "This is some random text, I would like £4.99 for this item, please call me on 07828123456 for further information.";
var numberReplace = new Regex(@"((\d){3}-1234567)|((\d){3}\-(\d){3}\-4567)|((\d){3}1234567)");
stringContainingPhoneNumber = numberReplace.Replace(stringContainingPhoneNumber, "[TELEPHONE REMOVED]");
Kara
  • 6,115
  • 16
  • 50
  • 57
  • 1
    Do you know the exact formatting of the telephone numbers you want to remove? – Nicolas R Jul 30 '14 at 08:34
  • Id does not work because phone number in you example ends with `123456` while your pattern has `1234567` – Konrad Kokosa Jul 30 '14 at 08:35
  • These links might be helpfull: http://blog.codinghorror.com/regex-performance/ and http://blogs.msdn.com/b/bclteam/archive/2010/06/25/optimizing-regular-expression-performance-part-i-working-with-the-regex-class-and-regex-objects.aspx – Remko Jul 30 '14 at 08:36
  • Have a look at the second answer to this post http://stackoverflow.com/questions/123559/a-comprehensive-regex-for-phone-number-validation – Paul Zahra Jul 30 '14 at 08:47
  • There are a variety of formats that people could use, as the input will be from another websites free text input. – David Wilson Jul 30 '14 at 09:42

4 Answers4

1

You may want to use a phone formatting library to identify valid phone numbers. Than you can replace them with whatever you want. You may use

http://blog.appharbor.com/2012/02/03/net-phone-number-validation-with-google-libphonenumber

virusrocks
  • 861
  • 1
  • 5
  • 19
0

This should work for your regex.

\d{11}[\s\d-]+

The number in the regex will match number sequences of that length in the string.

moikey
  • 353
  • 3
  • 7
  • 16
0

Just change your regex to ,

var numberReplace = new Regex(@"((\d){3}-1234567)|((\d){3}\-(\d){3}\-4567)|((\d){3}1234567)|(\b(\d){5}123456\b)");

Your regex won't work because in the input string, phone number contains 11 digits but in your pattern , there are only 10 digits. An also it isn't ends with 123456

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
0

Assuming UK numbers, for example:

"This is some random text, I would like £4.99 for this item, please call me on 07828123456 for further information or send a fax through (020) 2341 0231 or on (01204) 54203."

This should get just the phone numbers:

[\d\s-\(\)]{10,}

Tyress
  • 3,573
  • 2
  • 22
  • 45