Solved

Need to retrieve the first number in a string

Posted on 2013-05-10
3
458 Views
Last Modified: 2013-05-10
Hello Experts,

I need a mechanism to retrieve the first number in a string.

Ex: "This is a string 123 with numbers in it 456 ok?"

In the above example I'd need to retrieve 123 & ignore the 456.

Many Thanks,
Moe
0
Comment
Question by:mauricerichard
[X]
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
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 300 total points
ID: 39155205
The expression "\d+" combined with the Match method will give you this.

e.g.

using System.Text.RegularExpressions;

...

string number = Regex.Match(source, @"\d+").Value;

Open in new window


There is no error handling in the above, so if there is a chance that the target string contains no number, then the above needs some adjustment.
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 200 total points
ID: 39155206
0
 
LVL 1

Author Closing Comment

by:mauricerichard
ID: 39155284
Great answers, thanks!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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…

739 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