regex problem [modified]
-
Hi, I am using regextest program and filtering out text using below regex (?<=\<div class="middleadimggold"\>).*?(?=\</div\>) now I would like to include this in my c# code so that i can write only the text that matches. I am unsuccessfully doing this by using below code. I think something is not right and its not filtering out the data.
string match = @"(?<=\<div class=""middleadimggold""\>).*?(?=\</div\>)";
using (StreamWriter sw = File.AppendText(@"c:\\logs\\data.txt")) { if (Regex.IsMatch(this.Text,match)) { sw.WriteLine(this.Text); sw.Close(); } }
could someone please help. I think its something to do with inverted comma as when i ran the code in debug mode match becomes (?<=\\<div class=\"middleadimggold\"\\>).*?(?=\\</div\\>) if I put above expression in the regex software nothing is being returned. Thank you,
modified on Wednesday, January 13, 2010 12:36 AM
-
Hi, I am using regextest program and filtering out text using below regex (?<=\<div class="middleadimggold"\>).*?(?=\</div\>) now I would like to include this in my c# code so that i can write only the text that matches. I am unsuccessfully doing this by using below code. I think something is not right and its not filtering out the data.
string match = @"(?<=\<div class=""middleadimggold""\>).*?(?=\</div\>)";
using (StreamWriter sw = File.AppendText(@"c:\\logs\\data.txt")) { if (Regex.IsMatch(this.Text,match)) { sw.WriteLine(this.Text); sw.Close(); } }
could someone please help. I think its something to do with inverted comma as when i ran the code in debug mode match becomes (?<=\\<div class=\"middleadimggold\"\\>).*?(?=\\</div\\>) if I put above expression in the regex software nothing is being returned. Thank you,
modified on Wednesday, January 13, 2010 12:36 AM
Pasted your code into my app, and removed the StreamWriter stuff (otherwise identical)
private void button1\_Click(object sender, EventArgs e) { string text = tbPath.Text; string match = @"(?<=\\<div class=""middleadimggold""\\>).\*?(?=\\</div\\>)"; if (Regex.IsMatch(text, match)) { MessageBox.Show(text); } }
It works for me if I paste
<div class="middleadimggold">Data</div>
into the text box. The match works, and I get the message box - what doesn't happen with you?
All those who believe in psycho kinesis, raise my hand.
-
Pasted your code into my app, and removed the StreamWriter stuff (otherwise identical)
private void button1\_Click(object sender, EventArgs e) { string text = tbPath.Text; string match = @"(?<=\\<div class=""middleadimggold""\\>).\*?(?=\\</div\\>)"; if (Regex.IsMatch(text, match)) { MessageBox.Show(text); } }
It works for me if I paste
<div class="middleadimggold">Data</div>
into the text box. The match works, and I get the message box - what doesn't happen with you?
All those who believe in psycho kinesis, raise my hand.
-
Ok, but if teh Regex bit works her, then it will be working there. Which implies it is your file, streamwriter, or the data that is faulty. Are you getting any exceptions? Are you sure your data contains the match string? Have you tried a MessageBox or similar to check the data and the Regex match?
All those who believe in psycho kinesis, raise my hand.
-
Ok, but if teh Regex bit works her, then it will be working there. Which implies it is your file, streamwriter, or the data that is faulty. Are you getting any exceptions? Are you sure your data contains the match string? Have you tried a MessageBox or similar to check the data and the Regex match?
All those who believe in psycho kinesis, raise my hand.
-
Hi, I am using regextest program and filtering out text using below regex (?<=\<div class="middleadimggold"\>).*?(?=\</div\>) now I would like to include this in my c# code so that i can write only the text that matches. I am unsuccessfully doing this by using below code. I think something is not right and its not filtering out the data.
string match = @"(?<=\<div class=""middleadimggold""\>).*?(?=\</div\>)";
using (StreamWriter sw = File.AppendText(@"c:\\logs\\data.txt")) { if (Regex.IsMatch(this.Text,match)) { sw.WriteLine(this.Text); sw.Close(); } }
could someone please help. I think its something to do with inverted comma as when i ran the code in debug mode match becomes (?<=\\<div class=\"middleadimggold\"\\>).*?(?=\\</div\\>) if I put above expression in the regex software nothing is being returned. Thank you,
modified on Wednesday, January 13, 2010 12:36 AM
-
makes no difference how so ever. my regex works fine in a regextest tool its just only when i try to filter the content via c# it doesnt work. here is my code
string fName = @"data.txt";//path to text file
StreamReader testTxt = new StreamReader(fName);
string allRead = testTxt.ReadToEnd();//Reads the whole text file to the end
testTxt.Close(); //Closes the text file after it is fully read.
string regMatch = @"(?<=\<div class=""middleadimggold""\>).*?(?=\</div\>)"; //string to search for inside of text file. It is case sensitive.
if (Regex.IsMatch(allRead, regMatch))//If the match is found in allRead
{
Console.WriteLine("found\n");} else { Console.WriteLine("not found\\n"); }
part of the content that should match
<div class="middleadimggold">
<a class="asdf" href="http://www.asdf"> asdf</a>
<p>
asdf</p>
<p>asdf</p><p>asdf</p><p><span class="blue">asdf</span> <a class="de" href="http://www.4asdf"><span class="a">(See on map)</span></a></p> <p><span class="blue">asdf</span></p> <span class="xx">
asdf
</span></div>
please note the spacing in the content, not sure if it matters?
-
does it works if there is a big space between <div class="middleadimggold"> and closing of
? a typical example is as below: <div class="middleadimggold"> <a href="bla">bla
notice the spaces after initial div
Ah! It's your data! Looking at at your other post, it isn't the big white space - it's the newlines. You need to make a small change to your app:
if (Regex.IsMatch(allRead, regMatch))//If the match is found in allRead
becomes:
if (Regex.IsMatch(allRead, regMatch, RegexOptions.Singleline))//If the match is found in allRead
Should fix it.
All those who believe in psycho kinesis, raise my hand.
-
Hi, I am using regextest program and filtering out text using below regex (?<=\<div class="middleadimggold"\>).*?(?=\</div\>) now I would like to include this in my c# code so that i can write only the text that matches. I am unsuccessfully doing this by using below code. I think something is not right and its not filtering out the data.
string match = @"(?<=\<div class=""middleadimggold""\>).*?(?=\</div\>)";
using (StreamWriter sw = File.AppendText(@"c:\\logs\\data.txt")) { if (Regex.IsMatch(this.Text,match)) { sw.WriteLine(this.Text); sw.Close(); } }
could someone please help. I think its something to do with inverted comma as when i ran the code in debug mode match becomes (?<=\\<div class=\"middleadimggold\"\\>).*?(?=\\</div\\>) if I put above expression in the regex software nothing is being returned. Thank you,
modified on Wednesday, January 13, 2010 12:36 AM