Regular expression ignoring white space

Posted on 2010-09-11
Last Modified: 2012-06-27
I have a regular expression
which I use to check for a string of >= 15 figits e.g. 1868477631712341234
I now need to be able to validate the same string when it may contain spaces and I want to ignore the spaces. So that
1868 477 6317 1234 1234
is also a valid string.
Question by:PugwashXP
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 84

Expert Comment

ID: 33652347

Expert Comment

ID: 33652705
Ozo's solution is almost valid, except the spaces count against the 15 count.  You are going to need to do some scripting validation post regex.

I would suggest:

Digit, space or dash

Then in scripting:

Use a regex to replace any non 0-9 characters with null: [^0-9]
Then test the result to make sure your string length is 15.

If you need more specific help with a given language, let me know.
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 33653512
requires >= 15  digits
requires no digits
Removing non digits is still a good idea before doing any other processing on the validated result.

Author Closing Comment

ID: 33653697
Thanks - works just right

Author Comment

ID: 33653700
Thanks guys - all I needed was the regex for validation - processing was not the issue.

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question