Positive expression matches an HTML tag's property value _ Regular expression

Source: Internet
Author: User
Tags closing tag html tags regular expression

Regular expressions are necessary skills to do text parsing work. such as Web server log analysis, Web front-end development and so on. Many advanced text editors support a subset of regular expressions, and familiarity with regular expressions can often make some of your work more effective. For example, the number of lines of statistical code, only one is fixed. The matching of nested HTML tags is a difficult topic in the application of regular expressions, because it involves more regular syntax and is more difficult. So there is more value in research.

Today, due to the needs of the work, we need to get the attribute value of the HTML tag, and immediately think of the regular expression, labeled as follows:

<circle id= "Ap_test" cx= "cy=" "r=" 2 "stroke=" Black "stroke-width=" 0 "fill=" red "/> <circle
id=" AP _test "cx=" 201 "cy=" 2001 "r=" 2 "stroke=" Black "stroke-width=" 0 "fill=" red "/> <circle id="
ap_test "cx=" "" Cy " = "2002" r= "2" stroke= "Black" stroke-width= "0" fill= "red"/> <circle id= "ap_test
" cx= "203" cy= "2003" R= "2" Stroke= "BLACK" stroke-width= "0" fill= "Red"/>

Need to get the CX, CY attribute values for the <circle/> tag, and then write one:

$circle is the contents of the circle tag above

Preg_match_all ('/<\s*circle\s+[^>]*?cx\s*=\s* (\ ' |\ ') (. *?) \\1[^>]*?cy\s*=\s* (\ ' |\ ') (. *?) \\1[^>]*?\/?\s*>/i ', $circle, $arr);
Var_dump ($arr);

$arr [2] is a value of CX, $arr [4] is the value of CY.

Here is a description of the following regular matching closed HTML tags (support nesting)

Any complex regular expression is composed of simple subexpression, in order to write complex regular, on the one hand need to have the foundation of simple, on the other hand, we need from the point of view of the regular engine. On the principle of the regular engine, recommended "Mastering Regular Expression" Chinese name is "proficient in regular expression." A very good book.

OK, first identify the problem we're trying to solve--find out the innerHTML of the tag for a particular ID from a section of HTML text.

The biggest difficulty here is that HTML tags support nesting, how can you find the corresponding closed tag tag?

We can think like this, first match the front start tag, assuming that the Div bar (<div), and then once encountered nested Div, "Press on the stack", then if you encounter a div closed tag, "pop-up stack." If there is nothing in the stack when the closing tag is encountered, the end tag is the correct closing label.

The reason I am able to think this way is because I understand the characteristics of the regular, and I know that the balanced group in the regular can implement what I have just said "stack" operation. So, if we want to write complex regular expressions, we need to know at least some of the advanced features of the regular, so that we can think of problems in a direction.

================================

A regular expression that matches any closed HTML tag:

< ( 
 

If you want to match only div tags, you can use the following regular expression:

< (<HTMLTAG>DIV) [^>]*?> (?<nested><\k 
 

Yes, you can change the div to any HTML tag you want to match.

If you want to match multiple HTML tags at the same time, you can use the following regular expression:
< (?You can also continue to add more tags to match

If you want to match a label that contains an ID, you can use the following regular expression:

< (?  
 

This is a regular match for any HTML tag with an ID of footer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.