我有以下字符串
{class:"table table-striped"},c.a.createElement("thead",{class:"thread-dark"},c.a.createElement("tr",null,c.a.createElement("th",{scope:"col"},"Round 1"),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}))),c.a.createElement("tbody",null,c.a.createElement("tr",null,c.a.createElement("td",null,"Parc des Princes",c.a.createElement("br",null),"Paris"),c.a.createElement("td",{align:"right"},"France ",c.a.createElement("img",{src:"img/RoundFlags/France.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"},c.a.createElement("a",{href:"/fra-kor"},"4 - 0")),c.a.createElement("td",{align:"left"},c.a.createElement("img",{src:"img/RoundFlags/Korea.png",width:"50",hspace:"20"})," Korea"),c.a.createElement("td",null,"Group A")),c.a.createElement("tr",null,c.a.createElement("td",null,"Roazhon Park",c.a.createElement("br",null),"Rennes"),c.a.createElement("td",{align:"right"},"Germany ",c.a.createElement("img",{src:"img/RoundFlags/Germany.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"},c.a.createElement("a",{href:"/deu-chn"},"1 - 0")),c.a.createElement("td",{align:"left"},c.a.createElement("img",{src:"img/RoundFlags/China.png",width:"50",hspace:"20"})," China"),c.a.createElement("td",null,"Group B")),c.a.createElement("tr",null,c.a.createElement("td",null,"Stade Oceane",c.a.createElement("br",null),"Le Havre"),c.a.createElement("td",{align:"right"},"Spain ",c.a.createElement("img",{src:"img/RoundFlags/Spain.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"}
我正在尝试检索2个相同子字符串之间的所有字符,即c.a.createElement("tr"
和c.a.createElement("tr"
之间的所有字符作为列表。
我的尝试是使用以下正则表达式模式:
c\.a\.createElement\("tr"(.*?)c\.a\.createElement\("tr"
没有跳过序列之间的每一个,如图所示:
在上面,您可以看到第二个比赛1组0(浅蓝色)后面没有组1(绿色),尽管后面有c.a.createElement("tr"
(比赛2深蓝色)。
如果可以使用正则表达式,请访问以下网址:https://regex101.com/r/xzbHBU/1/
例如,我尝试了各种环顾方式
(?<=c\.a\.createElement\("tr")(.*?)(?!c\.a\.createElement\("tr")
并添加re.DOTALL
标志;所有这些都因太过折磨而惨遭失败。
有人可以帮助我编写适当的正则表达式来检索我期望中描述的所有组吗?
Python:
import re
s = '{class:"table table-striped"},c.a.createElement("thead",{class:"thread-dark"},c.a.createElement("tr",null,c.a.createElement("th",{scope:"col"},"Round 1"),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}))),c.a.createElement("tbody",null,c.a.createElement("tr",null,c.a.createElement("td",null,"Parc des Princes",c.a.createElement("br",null),"Paris"),c.a.createElement("td",{align:"right"},"France ",c.a.createElement("img",{src:"img/RoundFlags/France.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"},c.a.createElement("a",{href:"/fra-kor"},"4 - 0")),c.a.createElement("td",{align:"left"},c.a.createElement("img",{src:"img/RoundFlags/Korea.png",width:"50",hspace:"20"})," Korea"),c.a.createElement("td",null,"Group A")),c.a.createElement("tr",null,c.a.createElement("td",null,"Roazhon Park",c.a.createElement("br",null),"Rennes"),c.a.createElement("td",{align:"right"},"Germany ",c.a.createElement("img",{src:"img/RoundFlags/Germany.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"},c.a.createElement("a",{href:"/deu-chn"},"1 - 0")),c.a.createElement("td",{align:"left"},c.a.createElement("img",{src:"img/RoundFlags/China.png",width:"50",hspace:"20"})," China"),c.a.createElement("td",null,"Group B")),c.a.createElement("tr",null,c.a.createElement("td",null,"Stade Oceane",c.a.createElement("br",null),"Le Havre"),c.a.createElement("td",{align:"right"},"Spain ",c.a.createElement("img",{src:"img/RoundFlags/Spain.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"}'
p = re.compile(r'c\.a\.createElement\("tr"(.*?),c.a.createElement\("tr"')
matches = p.findall(s)
print(len(matches))
答案 0 :(得分:1)
使用先行和后退
例如:
import re
s = '{class:"table table-striped"},c.a.createElement("thead",{class:"thread-dark"},c.a.createElement("tr",null,c.a.createElement("th",{scope:"col"},"Round 1"),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}),c.a.createElement("th",{scope:"col"}))),c.a.createElement("tbody",null,c.a.createElement("tr",null,c.a.createElement("td",null,"Parc des Princes",c.a.createElement("br",null),"Paris"),c.a.createElement("td",{align:"right"},"France ",c.a.createElement("img",{src:"img/RoundFlags/France.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"},c.a.createElement("a",{href:"/fra-kor"},"4 - 0")),c.a.createElement("td",{align:"left"},c.a.createElement("img",{src:"img/RoundFlags/Korea.png",width:"50",hspace:"20"})," Korea"),c.a.createElement("td",null,"Group A")),c.a.createElement("tr",null,c.a.createElement("td",null,"Roazhon Park",c.a.createElement("br",null),"Rennes"),c.a.createElement("td",{align:"right"},"Germany ",c.a.createElement("img",{src:"img/RoundFlags/Germany.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"},c.a.createElement("a",{href:"/deu-chn"},"1 - 0")),c.a.createElement("td",{align:"left"},c.a.createElement("img",{src:"img/RoundFlags/China.png",width:"50",hspace:"20"})," China"),c.a.createElement("td",null,"Group B")),c.a.createElement("tr",null,c.a.createElement("td",null,"Stade Oceane",c.a.createElement("br",null),"Le Havre"),c.a.createElement("td",{align:"right"},"Spain ",c.a.createElement("img",{src:"img/RoundFlags/Spain.png",width:"50",hspace:"20"})),c.a.createElement("td",{className:"align-middle",align:"center"}'
p = re.compile(r'(?<=c\.a\.createElement\("tr")(.*?)(?=,c.a.createElement\("tr")')
matches = p.findall(s)
print(matches)
输出:
3