开发者

Regex to detect incomplete HTML

开发者 https://www.devze.com 2023-01-17 15:48 出处:网络
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

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 th开发者_高级运维e 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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号