469,927 Members | 1,843 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,927 developers. It's quick & easy.

Regex

Hi

I'm trying to do something which I think is pretty simple, but really I
don't know what I'm doing :-)

I have a string like this

HELLO WORLD
25 ERRORS

I want to search the string and return and line with the word ERRORS

25 ERRORS
I've tried this, but it's not working... Can oneone help a dummy please?
Thanks
Regex reg1 = new Regex("HELLO WORLD" + Environment.NewLine + "25 ERRORS");

Match m;

m = reg1.Match(".ERRORS.");

textBox2.Text = m.Value;


Jul 13 '06 #1
4 3236
Hi there,
Regex is one of those things that takes some time to get used to. If I
read your question right, you want to find a line that looks like
<beginning of line><some set of digits><the word "ERRORS">

If that's what you want, take a look at this code:

string stringToSearch = "Don't match this line" + Environment.NewLine
+ "Or this line" + Environment.NewLine + "25 ERRORS";
string patternToMatch = "^[0-9]+ ERRORS";
Regex reg1 = new Regex(patternToMatch,RegexOptions.Multiline);
Match m = reg1.Match(stringToSearch);
if (m.Success)
MessageBox.Show(m.Value);

The Regex in this case breaks down like this:

^ means match the beginning of a line
[0-9]+ means match any number of digits
ERRORS means just match the string

Hope that helps,
John

..... wrote:
Hi

I'm trying to do something which I think is pretty simple, but really I
don't know what I'm doing :-)

I have a string like this

HELLO WORLD
25 ERRORS

I want to search the string and return and line with the word ERRORS

25 ERRORS
I've tried this, but it's not working... Can oneone help a dummy please?
Thanks
Regex reg1 = new Regex("HELLO WORLD" + Environment.NewLine + "25 ERRORS");

Match m;

m = reg1.Match(".ERRORS.");

textBox2.Text = m.Value;
Jul 13 '06 #2
You have not explained your business rules.
I have a string like this
In what way is your string "like this?" What is similar? What is different?
Will it always be the same string? If not, what will be different?
I want to search the string and return and line with the word ERRORS
How do you know there will only be one line with the word "ERRORS" in it?
Again, what are the business rules? Will the word "ERRORS" always be
preceded by a number? Will there always be only a number followed by the
word "ERRORS?"

It looks to me like you probably don't need a Regular Expression at all, but
you haven't defined the problem well enough to answer.

Quite often, the greater part of any problem-solving process is simply
identifying the problem.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
"...." <...@nowhere.comwrote in message
news:ex**************@TK2MSFTNGP05.phx.gbl...
Hi

I'm trying to do something which I think is pretty simple, but really I
don't know what I'm doing :-)

I have a string like this

HELLO WORLD
25 ERRORS

I want to search the string and return and line with the word ERRORS

25 ERRORS
I've tried this, but it's not working... Can oneone help a dummy please?
Thanks
Regex reg1 = new Regex("HELLO WORLD" + Environment.NewLine + "25 ERRORS");

Match m;

m = reg1.Match(".ERRORS.");

textBox2.Text = m.Value;


Jul 13 '06 #3
..... <...@nowhere.comwrote:
Hi

I'm trying to do something which I think is pretty simple, but really I
don't know what I'm doing :-)

I have a string like this

HELLO WORLD
25 ERRORS

I want to search the string and return and line with the word ERRORS

25 ERRORS
I've tried this, but it's not working... Can oneone help a dummy please?
Thanks
Regex reg1 = new Regex("HELLO WORLD" + Environment.NewLine + "25 ERRORS");

Match m;

m = reg1.Match(".ERRORS.");

textBox2.Text = m.Value;
Try:

Regex rx = new Regex(".*ERRORS.*");
if (rx.IsMatch(testString))
{
textBox2.Text = testString;
}

I had to "guess" your intent with the example code you wrote, so let me know
if I am off and I can try to help further.

--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Jul 13 '06 #4
Thomas T. Veldhouse <ve*****@yahoo.comwrote:
.... <...@nowhere.comwrote:
>Hi

I'm trying to do something which I think is pretty simple, but really I
don't know what I'm doing :-)

I have a string like this

HELLO WORLD
25 ERRORS

I want to search the string and return and line with the word ERRORS

25 ERRORS
I've tried this, but it's not working... Can oneone help a dummy please?
Thanks
Regex reg1 = new Regex("HELLO WORLD" + Environment.NewLine + "25 ERRORS");

Match m;

m = reg1.Match(".ERRORS.");

textBox2.Text = m.Value;

Try:

Regex rx = new Regex(".*ERRORS.*");
if (rx.IsMatch(testString))
{
textBox2.Text = testString;
}

I had to "guess" your intent with the example code you wrote, so let me know
if I am off and I can try to help further.
Ah ... I see you have a newline and would like to break on numbers as well:

Regex rx = new Regex("^[0-9]+ ERRORS");
foreach (string str in testString.Split(Environment.Newline))
{
if (rx.IsMatch(str))
{
textBox2.Text = str;
break;
}
}
--
Thomas T. Veldhouse
Key Fingerprint: 2DB9 813F F510 82C2 E1AE 34D0 D69D 1EDC D5EC AED1

Jul 13 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Tim Conner | last post: by
20 posts views Thread by jeevankodali | last post: by
17 posts views Thread by clintonG | last post: by
6 posts views Thread by Extremest | last post: by
7 posts views Thread by Extremest | last post: by
3 posts views Thread by aspineux | last post: by
15 posts views Thread by morleyc | last post: by
reply views Thread by Karch | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.