簡體   English   中英

從字符串中獲取子字符串(單引號之間的字符)(Python)

[英]Getting a substring from a string (characters in between single quotes) (Python)

這是我目前擁有的字符串:

URL = "location.href='agent_specific_listing.php?sid=131184&mls=693010&a=103&site_id=540&page_current=1';"

我試圖用單引號拆分子字符串,以便這是結果:

new_url = 'agent_specific_listing.php?sid=131184&mls=693010&a=103&site_id=540&page_current=1'

我嘗試使用 re 和 findall 但我得到了空字符串:

print(re.findall(r"\(u'(.*?)',\)", URL)) // printed empty lists

請讓我知道我做錯了什么。 非常感謝。

print re.findall(r"\'(.*?)\'", URL)

因為這是您處理單引號的方式:

\'   matches a literal '
>>> print(re.findall(ur"'(.*?)'", URL))
['agent_specific_listing.php?sid=131184&mls=693010&a=103&site_id=540&page_current=1']
URL = "location.href='agent_specific_listing.php?sid=131184&mls=693010&a=103&site_id=540&page_current=1';"


newURL = URL.split('location.href=')[1]

print(newURL)

輸出:

'agent_specific_listing.php?sid=131184&mls=693010&a=103&site_id=540&page_current=1';

暫無
暫無

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

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