.NET Framework Bookmark and Share   
 index > Regular Expressions > Regular Expression that would include everything
 

Regular Expression that would include everything

Aloha,

I am trying to take a substring of a string and set it to another variable to be able to work with it. This is the code:

Regex divRegex = new Regex(@"<DIV id=compchart>(.*[\n*])</DIV>");



divSection = Convert.ToString(divRegex.Match(html));




What I am trying to do is the divSection string variable to one section of the html page which is the compchart div section. I need a regular expression between "<DIV id=compchart>" and "</DIV>" to be able to accept everything since the information contains html tags, attributes, and text. I thought the "." was a wildcard meaning any character except the "\n" so I added it in. This actually set my divSection string variable to Null or blank.

What is wrong with my regular expression that I am not getting that substring from the full html text?
spyguyhi

Did you know that divRegex.Match(html).Value will also give you the string? Or are you using Convert.ToString() in case there is no match? divRegex.Match(html).Success will provide a true or false indication...

divSection = (divRegex.Match(html).Success ? divRegex.Match(html).Value : "");

...might be clearer.


Les Potter, Xalnix Corporation, Yet Another C# Blog
  • Marked As Answer byspyguyhi Thursday, September 17, 2009 7:25 PM
  •  
xalnix

Ah! I think I figured out what I needed.

Regex divRegex = new Regex(@"<DIV id=compchart>(.*)</DIV>", RegexOptions.Singleline);
divSection = Convert.ToString(divRegex.Match(html));
Just using the ".*" and setting the RegexOptions.SingleLine seems to have worked :)
spyguyhi

Did you know that divRegex.Match(html).Value will also give you the string? Or are you using Convert.ToString() in case there is no match? divRegex.Match(html).Success will provide a true or false indication...

divSection = (divRegex.Match(html).Success ? divRegex.Match(html).Value : "");

...might be clearer.


Les Potter, Xalnix Corporation, Yet Another C# Blog
  • Marked As Answer byspyguyhi Thursday, September 17, 2009 7:25 PM
  •  
xalnix
The divRegex.Match(html).Value works so much better thank you for the solution.
spyguyhi

You can use google to search for other answers

Custom Search

More Threads

• help with regex
• Regular epressions union
• Need help in building a validation expression
• break letter groups and numbers
• Sequential letters and numbers: "abc" "123"
• Simple Regex question (hopefully!)
• Problem With Reg Pattern
• Extracting hyperlinks
• RegEx and URL's
• Help required on regular expresion for finding particular strin