我正在使用python3并尝试用regex提取字符串的特定部分。 这是字符串:
data = "'Star Wars: The Last Jedi (Theatrical Version)MPAA Rating: PG-13 (Parents Strongly Cautioned)|Closed Caption3.8 out of 5 stars4,738Prime Videofrom$2.99$2.99to rentStarring:Oscar Isaac,Mark Hamill,Daisy RidleyandJohn BoyegaDirected by:Rian JohnsonRuntime:151 minutes'"
只有“4738”这个数字总是出现在“Prime Videofrom$”之前,提取这个数字的最佳方法是什么? 这是我的密码:
import re
data = "'Star Wars: The Last Jedi (Theatrical Version)MPAA Rating: PG-13 (Parents Strongly Cautioned)|Closed Caption3.8 out of 5 stars4,738Prime Videofrom$2.99$2.99to rentStarring:Oscar Isaac,Mark Hamill,Daisy RidleyandJohn BoyegaDirected by:Rian JohnsonRuntime:151 minutes'"
reviews = re.findall("[stars][\d,]+\$",data)
print(reviews)
但我得到一张空名单:
[]
如何提取在完全匹配之前仅包含逗号的数字
^{} 对于单个匹配是最佳的:
考虑到您的条件“提取精确匹配前仅包含逗号的数字”
(?=Prime Videofrom\$)
-lookahead position断言,确保前面的匹配(数字序列)后跟Prime Videofrom$
用途:
相关问题 更多 >
编程相关推荐