簡體   English   中英

與PHP在Python中的preg_match相對應

[英]Counterpart to PHP’s preg_match in Python

我正計划將我的刮板之一移至Python。 我很喜歡在PHP中使用preg_matchpreg_match_all 我在Python中找不到類似於preg_match的合適函數。 有人可以幫我嗎?

例如,如果我想要得到的內容<a class="title"</a> ,我在PHP中使用下列功能:

preg_match_all('/a class="title"(.*?)<\/a>/si',$input,$output);

而在Python中,我無法找出類似的功能。

您在尋找python的re模塊

看一下re.findallre.search

正如您提到的那樣,您正在嘗試解析html,請使用html parsers 在python中有兩個選項,例如lxmlBeautifulSoup

看看這個為什么不應該用正則表達式解析html

您可能對閱讀Python正則表達式操作感興趣

我認為您需要這樣的東西:

output = re.search('a class="title"(.*?)<\/a>', input, flags=re.IGNORECASE)
    if output is not None:
        output = output.group(0)
        print(output)

您可以在正則表達式的開頭添加(?s)以啟用多行模式:

output = re.search('(?s)a class="title"(.*?)<\/a>', input, flags=re.IGNORECASE)
    if output is not None:
        output = output.group(0)
        print(output)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM