473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Anyone have a regex expression to create a numeric-only string from a formatted string?

I would like to quickly strip out all non-numeric characters from a string
(including whitespace) into a new string (i.e., remove the edit mask). Would
RegEx be the best way to do this and, if so, what is the syntax?
Nov 16 '05 #1
2 4523
Ray,
Regex would probably work, but why not just use a simple loop?

String SomeString = "blah044954blah";
StringBuilder NewString = new StringBuilder();
for (int i = 0;i < SomeString.Length;i++)
{
if (!(((int) SomeString[i]) >= 48 || ((int) SomeString[i]) <= 57)) //
Assuming ASCII of course
{
NewString.Append(SomeString[i]);
}
}
SomeString = NewString.ToString();

This isn't the cleanest code, but it is fast.

Take care.

--
Nathan

"Ray Stevens" <nf*@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I would like to quickly strip out all non-numeric characters from a string
(including whitespace) into a new string (i.e., remove the edit mask).
Would
RegEx be the best way to do this and, if so, what is the syntax?

Nov 16 '05 #2
Could be as simple as

Regex.Replace(" 12-34 ", @"\D", String.Empty)

If you don't need decimal or thousand separators, which you most like likely
do need... Then you're on your own :-)

HTH,
Alexander

"Ray Stevens" <nf*@nospam.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I would like to quickly strip out all non-numeric characters from a string
(including whitespace) into a new string (i.e., remove the edit mask).
Would
RegEx be the best way to do this and, if so, what is the syntax?

Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Phil396 | last post by:
I am trying to mix metacharacters and the literal versions in an expression and no matter what I try it does not work. Here is the code customer = Regex.Replace(customer,"$",""); I want to...
6
by: BigAl | last post by:
Perhaps someone here can help me out... RegEx: "^.*\d{5}(-\d{4})?.*$" Intended Purpose: To strip out the City/State/ZipCode line from a signature. Sample Text: Joe Jackson 131 W. 5th...
2
by: JebBushell | last post by:
I am trying to use the VS2003 regular expression Find tool to test regular expressions for use in a ASP.NET validator. The results I am getting are inconsistent . What works in VS does not...
2
by: JebBushell | last post by:
I see signs that the ASP.NET regular expression validator has a different instruction set that the Find utility in VS 2003. I am trying to use the VS2003 regular expression Find tool to test...
1
by: David | last post by:
I have rows of 8 numerical values in a text file that I have to parse. Each value must occupy 10 spaces and can be either a decimal or an integer. // these are valid - each fit in a 10 character...
5
by: =?Utf-8?B?SkF1bA==?= | last post by:
I am currently working on a project and need to get a return… even if that return is a failure. I must also add that I have no control over either the Regular Expression that will be used or the...
4
by: Henrik Dahl | last post by:
Hello! In my application I have a need for using a regular expression now and then. Often the same regular expression must be used multiple times. For performance reasons I use the...
1
by: TtfnJohn | last post by:
I have two small scripts that while on the surface should both work the problem is they don't. Here's the first one: import re testString = 'Thap,fpvi,*!wtyd@*.dip.t-dialin.net:*!ylx@*.dip.t-...
1
by: Cyber | last post by:
Hi. we have a feature where the user can create a regular expression to do a mask edit (pattern) on a specific field. This field cannot have more than 5 characters. We want to validate the...
3
by: William Gill | last post by:
I am not to sharp on my regular expressions because I haven't used them in quite a while. So I am relearning regex and the PHP regex functions at the same time. Which means when I screw up, I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.