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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.
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

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. ;)
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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.
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

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

rmtogetherAuthor Commented:
thanks a lot
käµfm³d 👽Commented:
NP. Glad to help :)
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.