Solved

Need to retrieve the first number in a string

Posted on 2013-05-10
3
456 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 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

Independent Software Vendors: 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
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…

756 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