用Python匹配HTML tag的時候, 和 有什麼區別?

時間 2021-06-02 22:37:15

1樓:小帥

div>testspan>span>.*?> : < ) 非貪婪匹配( < )

例如: /div>《術語叫貪婪匹配( <.*> : <

<

2樓:

* 號是乙個量詞

量詞後面加? 號表示非貪婪,也就是盡可能少的匹配* 可以是表示0 或者大於0個

所以 * 在貪婪的情況盡可能多的匹配

*? 就表示盡可能少的匹配

3樓:黃哥

請看"?" Matches 0 or 1 (greedy) of the preceding RE.

The special characters are:

"." Matches any character except a newline.

"^" Matches the start of the string.

"$" Matches the end of the string or just before the newline at

the end of the string.

"*" Matches 0 or more (greedy) repetitions of the preceding RE.

Greedy means that it will match as many repetitions as possible.

"+" Matches 1 or more (greedy) repetitions of the preceding RE.

"?" Matches 0 or 1 (greedy) of the preceding RE.

Non-greedy versions of the previous three special characters.

Matches from m to n repetitions of the preceding RE.

? Non-greedy version of the above.

"\\" Either escapes special characters or signals a special sequence.

Indicates a set of characters.

A "^" as the first character indicates a complementing set.

"|" A|B, creates an RE that will match either A or B.

Matches the RE inside the parentheses.

The contents can be retrieved or matched later in the string.

(?aiLmsux) Set the A, I, L, M, S, U, or X flag for the RE (see below).

Non-grouping version of regular parentheses.

(?P...) The substring matched by the group is accessible by name.

(?P=name) Matches the text matched earlier by the group named name.

A comment; ignored.

Matches if ... matches next, but doesn't consume the string.

Matches if ... doesn't match next.

(?<=...) Matches if preceded by ... (must be fixed length).

(?(?(id/name)yes|no) Matches yes pattern if the group with id/name matched,

python正則匹配和字串匹配,哪個效率更高?

靈劍 你用正規表示式的情況肯定是單純字串查詢滿足不了你的需求了,除此以外不要用正規表示式,當真的需要的時候你也沒有太多別的選擇,所以其實沒啥好比的。 yishen chen python m timeit import re pat re.compile r llo pat.search hello...

python入門用《Learn Python The Hard Way 》好還是《Learning Python》好?

華家池之貓 Learning Python其實是一本很好的書。雖然我一開始也抱怨寫的太繁雜,但是這本書有個好處就是知識點是相互串聯的。你第一次遇到一件東西理解不了,記不住,等過了幾張以後會發現又重新出現了。Learning Python作為嚴謹的課程教材以及作為小白入門或者想轉行cs的人讀都不太適合...

學習 Python 用哪本書好?

Nextchen Oreilly Progamming Python 涵蓋了語法入門,系統程式設計,圖形介面程式設計,網路程式設計,教你如何開發一款編輯器,如何編寫 Web等等!如果你還不知道學什麼好的話?你可以全部試一遍! Python快速教程,業界良心,比目前排名第一的廖雪峰的好,還有就是pyt...