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;
 
J

John Duval

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
 
K

Kevin Spencer

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

Thomas T. Veldhouse

..... said:
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.
 
T

Thomas T. Veldhouse

Thomas T. Veldhouse said:
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;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top