Coldfusion Detect and remove phone numbers from a string / input

Does anyone know of a routine that removes phone numbers from a string.
Ian WhiteOwner and FounderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

can you please give us more details ? what is the code? from where that data is coming ect
it will help us to understand ur problem
James RodgersWeb Applications DeveloperCommented:
You can use rereplace which uses a reg ex to do a find and replace, but you will need to determine the number formats to be replaced and the best way to process it.

rereplace(str_to_search, reg_ex, replace_with[, scope])

scope is usually all , default is 'one'

now you need to determine the number format of what you want to replace
US/Can phone numbers
Ian WhiteOwner and FounderAuthor Commented:
People can enter free format text.  They are not allowed to enter phone numbers in their text ad, but
I dont want to stop say 7 years   or 7 miles or 97 kilometers. They will be in all formats as I run a world wide site.

I found a regex for this for  php from another expert site.  I tried to translate this to coldfusion but it failed

So basically if they have numbers grouped together remove them

PHP working example

function removeEmailAndPhoneFromString($string) {
    // remove email
    $string = preg_replace('/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/','',$string);

    // remove phone
    $string = preg_replace('/([0-9]+[\- ]?[0-9]+)/','',$string);

    return $string;

echo removeEmailAndPhoneFromString('Lorem ipsum dolor sit amet, 555-123 555 consectetur adipiscing elit.');

Open in new window

My Attempt

  <!--- remove phone numbers   ---->    
                             <cfset LoadNotes = REReplaceNoCase
							 (LoadNotes,"/\+?[0-9][0-9()-\s+]{4,20}[0-9]/", "[removed]", "all")>
But I get CF error

Malformed regular expression "/\+?[0-9][0-9()-\s+]{4,20}[0-9]/".
Reason: Invalid [] range in expression.. <br>The error occurred on line

Open in new window

CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

(no points...)

Regex's aren't my strong suit, but ... I'm guessing CF is interpreting the middle dash "-" as the start of a range.  Try escaping it:


... instead of

Ian WhiteOwner and FounderAuthor Commented:
Thanks for your effort , I changed it but it did not do anything.

I found another solution that appears to work

I would keep it simple:

Two numbers, one or more of whitespace, numbers or a hyphen.

Require more characters with:

(two numbers and 5 or more of whitespace, numbers of hyphens) which will reduce the number of mis-hits.

These will include an extra space following the phone-number, but the results could be trimmed.

Rather than trim, though, I would remove the hyphens and whitespace and count the number of digits leftover to recognise them as phone numbers.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ian WhiteOwner and FounderAuthor Commented:
I also found another solution. I was just using wrong search term in google.

Since while you could try to write some crazy REGEX that would qualify each number based on it's country code, dialing prefix, etc for matching in your purposes this is not needed and would be a waste of time. From a Bayesian approach the longer numbers tend to be 18 characters (Argentina mobile numbers) with possibility of a leading + character followed by numbers [0-9] or \d, parenthesis (), brackets [] and possibly spaces , periods ., or hyphens - and one obscure format with a /.

\b\+?[0-9()\[\]./ -]{7,17}\b
Yeah, I was just addressing the syntax error.  Regex's aren't my strong suit, so I would've been surprised if it worked out of the box ;-)   Glad you found something that works.  Thanks for posting the answer.
Ian WhiteOwner and FounderAuthor Commented:
I tested it and it works
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ColdFusion Language

From novice to tech pro — start learning today.