Solved

Need to retrieve the first number in a string

Posted on 2013-05-10
3
447 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
3 Comments
 
LVL 74

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 69

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now