tags:

views:

73

answers:

3

I'm trying to write a search and replace regex that will detect whether HTML that has been returned by a web request is complete. I have had cases when the server returns incomplete HTML (half of the page), so I want to detect that in the client and request the page again.

I was thinking the regex could look for the presence of <html[^>]*>, and then the absence of </html>. The replace part would then replace the whole HTML with a bit of special text.

I can't just check for the absence of </html> because the returned data might be a text file, and I can't check MIME types.

Any ideas? I just can't wrap my head around the look-behinds this would require. I'm not trying to parse HTML, just searching for bits of text, which is what regexes are for, right?

EDIT:

The regexes will be run by C#, but I write them in a regex editor. I can only use a search and replace regex to solve this, nothing else.

+2  A: 

This is not possible using RegEx. HTML is not a regular language, so incomplete pages cannot be verified by RegEx.

See here for why parsing HTML with RegEx is a bad idea.

Use a validating HTML parser for your platform to load the HTML and check for validation errors.

You need to figure out if a file is HTML or not before you try to detect whether it is incomplete HTML. You may be able to do this using the file extension, if available, however you did not provide enough information about your environment for any recommendation to be made.


It is easy enough to find whether <html and </html> exist in a string and if not return a "special string". Normal string functions should suffice - no need for RegEx.

Oded
I'm not asking for much. I'm not parsing HTML. All I need is a regex that would insert a bit of text if there is an <html> string, but no </html> string.
Ed
@Ed - then please _edit_ your question and clarify it.
Oded
If I could, I would use something else, but for this problem I can only use a search and replace regex.
Ed
@Ed - again, please _edit_ your question and _add_ these bits of information to it. It will help anyone reading your question to answer it better.
Oded
+3  A: 

Oded is correct. You cannot parse HTML with regex. But of course you can see whether some (multiline) string contains <html> not followed by </html>. If you are sure that whatever your web request returns will be consistent and not contain any weird things like html tags inside comments, then

<html\b[^>]*>(?:(?!<\s*/\s*html).)*\Z

will find such a string, if you set the "dot matches newlines" option. How to do this depends on the regex implementation which you didn't provide yet.

<html\b[^>]*>          # match <html> tag
(?:                    # match the following:
  (?!<\s*/\s*html)     # If it's impossible to match </html here
  .                    # then match any character
)*                     # zero or more times.
\Z                     # Then assert that we are indeed at the end of the string
Tim Pietzcker
See [Bart K.’s comment](http://stackoverflow.com/questions/3785861/regex-to-detect-incomplete-html#comment-4010395).
Gumbo
@Gumbo: Of course I did. I do know the limitations (and have mentioned them). But in his case it seems workable since he can exclude cases that would trip this regex up.
Tim Pietzcker
This hit the spot! Thank you.
Ed
A: 

In general, regular expressions are not suitable to describe HTML because regular expressions can only describe regular languages but HTML is not a regular language. The fact that HTML allows to nest elements in an arbitrary depth makes it irregular.

Although there are regular expression implementations that support recursive patterns (e.g. PCRE, .NET, et al.), it would require a horrible complex regular expression to describe every valid HTML code.

Gumbo