simple regular expression

Hi,

Can someone help me a simple regular expression question

I would like the regular expression to help me find the year from the input string
<for example>

str = " this is year 2010", and regular expression will return me 2010. in order words it only focus on find 4 digits number from the input string

thanks in advance
rmtogetherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
The regular expression matching that would be \d{4}, the \d meaning a digit of 0-9, and the {4} means the preceeding element (in this case, a digit) must occur four times.
0
Pui_YunCommented:
Hi rmtogether,
You can use this regex to find four digits together:
\b\d{4}\b

\b - boundary
\d - digit

You'll need:
using System.Text.RegularExpressions;

at the top of your program.

See attached code.

Hope this helps.
P.
string strInput = @"this is year 2010";
            foreach (Match mYearMatch in new Regex(@"\b\d{4}\b").Matches(strInput))
            {
                MessageBox.Show(mYearMatch.Value);
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Todd GerbertIT ConsultantCommented:
Doh... P's right!  Without the word boundary \d{4} would match the first four digits of any number regardless of length.
Just goes to show, there's no such thing as a simple regular expression. ;)
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

rmtogetherAuthor Commented:
thanks, i am new to this

could you also show me how to do this without using "foreach". if I am only has 1 input string at a time. like the input is from textbox.
0
käµfm³d 👽Commented:

string test = " this is year 2010";
string pattern = @"\b(\d{4})\b";
string result;

if (System.Text.RegularExpressions.Regex.IsMatch(test, pattern)
{
	result = System.Text.RegularExpressions.Regex.Match(test, pattern).Groups[0].Value;
}

Open in new window

0
käµfm³d 👽Commented:
Correction:
string test = " this is year 2010";
string pattern = @"\b(\d{4})\b";
string result;

if (System.Text.RegularExpressions.Regex.IsMatch(test, pattern)
{
	result = System.Text.RegularExpressions.Regex.Match(test, pattern).Groups[1].Value;
}

Open in new window

0
rmtogetherAuthor Commented:
thanks a lot
0
käµfm³d 👽Commented:
NP. Glad to help :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.