簡體   English   中英

Python正則表達式返回括號內字符串的位置

[英]Python regular expression return positions of string inside the parenthesis

使用正則表達式,我試圖獲取括號內字符串的位置。

例如,我想獲得“ Home Depot”的位置;

sent = "Sales to two of the segment's customers, The Home Depot and Lowe's Home Improvement Warehouse, accounted for greater than 10% of the Corporation's consolidated sales for 2004, 2003, and 2002."

regex_ = re.compile("Sales to two of the segment's customers, The (Home Depot)

然而,

regex_.search(sent).span()

返回(0, 55)不返回(0, 55) (45, 55)

由於可能發送了多個“家re.search('Home Depot', sent).span() ,因此我無法使用re.search('Home Depot', sent).span() ,它可能無法返回我要尋找的家得寶的確切位置。

如果要獲取括號中文本的位置,則需要指定要匹配的第一組作為span()的參數:

sent = "Sales to two of the segment's customers, The Home Depot and Lowe's Home Improvement Warehouse, accounted for greater than 10% of the Corporation's consolidated sales for 2004, 2003, and 2002."

regex_ = re.compile("Sales to two of the segment's customers, The (Home Depot)

regex_.search(sent).span(1)

有關匹配對象和span請參見python文檔。

使用積極的眼光:

sent = "Sales to two of the segment's customers, The Home Depot and Lowe's Home Improvement Warehouse, accounted for greater than 10% of the Corporation's consolidated sales for 2004, 2003, and 2002."
regex_ = re.compile(r"(?<=Sales to two of the segment's customers, The )Home Depot")
print(regex_.search(sent).span())

輸出:

(45, 55)

您的正則表達式是正確的。 但是,您需要的是整個比賽的位置,而不是子比賽的位置。 要獲得第一個子匹配項的位置,請使用span(1)

>>> regex_ = re.compile("Sales to two of the segment's customers, The (Home Depot)")
>>> regex_.search(sent).span(1)
(45, 55)

暫無
暫無

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

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