Solved

Need to retrieve the first number in a string

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

815 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

7 Experts available now in Live!

Get 1:1 Help Now