Python serial 50 greedy matching and XPath introduction


1、 Greed and non greed

1. Greedy: as many matches as possible, (*) indicates greedy match

2. Non greedy: find the minimum content that meets the conditions Non greedy

3. Regular uses greedy matching by default

import re

title = u"nameage"

P1 = re. Compile (R ". *") (greedy mode)

P2 = re. Compile (R ". *?") non greedy mode

m1 =



m2 =



1. Definition: a set of rules / Languages for finding information in XML files, according to the elements of XML
Document help:
2. XPath development tools
Open source XPath expression editing tool: xmlquire
Chrome plug in: XPath
Firefox plug in: XPath
3. How to select nodes in XML file
(1) NodeName: Selects all children of this node
(2) /: pick from root
Example / student: no result
/School: select the school node
(3) //: select nodes regardless of location
Example: // age: select three nodes, and the general composition list returns
(4) .: select the current node
(5) ..: select the parent node of the current node
(6) @: select Properties
(7) Searching in XPath is generally based on the path method
School / Teacher: return to the teacher node
School / student: return two student nodes
//Student: select nodes of all students, regardless of location
School // age: select all age nodes in the school descendants
//@Other: select other attribute
//Age [@ details]: select the age element with attribute details















4. predicate
/School / student [1]: select the first student node under school
/School / student [last()]: select the last student node under school
/School / student [last() - 1]: select the penultimate student node under school
/School / student [position() < 3]: select the first two nodes under school
//Student [@ score]: select the student node with attribute score
//Student [@ score = "99"]: select the student node with attribute score and attribute value 99
//Student [@ score] / age: select the child node age of the student node with attribute score
5. Some operations in XPath
(1) | or
For example: // student [@ score] | // Teacher: select the student node or teacher node with attribute score
(2) Other unusual XPath symbols include +. -. *, div, ><
Two, source code
2. CSDN:
3. Blog Park:
4. welcome to pay attention to WeChat public number: Fourier transform, personal public number, only for learning exchanges, background reply "gift package", access to big data learning materials.