Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. regex problem [modified]

regex problem [modified]

Scheduled Pinned Locked Moved C#
regexhelpcsharpdebuggingquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    uglyeyes
    wrote on last edited by
    #1

    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

    OriginalGriffO C R 3 Replies Last reply
    0
    • U uglyeyes

      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

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      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.

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      U 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        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.

        U Offline
        U Offline
        uglyeyes
        wrote on last edited by
        #3

        it doesnt write the matching text to a file. so i do need streamwriter stuff working

        OriginalGriffO 1 Reply Last reply
        0
        • U uglyeyes

          it doesnt write the matching text to a file. so i do need streamwriter stuff working

          OriginalGriffO Offline
          OriginalGriffO Offline
          OriginalGriff
          wrote on last edited by
          #4

          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.

          "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
          "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

          U 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            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.

            U Offline
            U Offline
            uglyeyes
            wrote on last edited by
            #5

            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

            OriginalGriffO 1 Reply Last reply
            0
            • U uglyeyes

              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

              C Offline
              C Offline
              carlecomm
              wrote on last edited by
              #6

              Remove '\', there is no need to use '\' before '>'.

              U 1 Reply Last reply
              0
              • C carlecomm

                Remove '\', there is no need to use '\' before '>'.

                U Offline
                U Offline
                uglyeyes
                wrote on last edited by
                #7

                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>
                		 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<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?

                1 Reply Last reply
                0
                • U uglyeyes

                  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

                  OriginalGriffO Offline
                  OriginalGriffO Offline
                  OriginalGriff
                  wrote on last edited by
                  #8

                  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.

                  "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                  "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                  1 Reply Last reply
                  0
                  • U uglyeyes

                    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

                    R Offline
                    R Offline
                    Ravi Sant
                    wrote on last edited by
                    #9

                    Answer as by Original.. stated above. correct the string>> string match = @"(?<=

                    ).*?(?=

                    )";

                    ♫ 99 little bugs in the code, 99 bugs in the code We fix a bug, compile it again 101 little bugs in the code ♫

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups