为什么这个regex表达式在Python和Javascript中给出两个不同的结果?

2024-09-26 22:49:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下字符串:

[?]The Taliban Have Gone High-Tech. That Poses a Dilemma for the U.S. The frequency and ferocity of the nighttime Taliban attacks are linked to attempts by Afghan forces, based in small checkpoints across the country, to hold territory that has been wrested away from the militants. Afghan troops said the missing devices were reported as "Battle losses," but could not support that claim with any proof or records to explain where or when they were left behind, according to the documents. Over the summer and fall, the Afghan National Army suffered 15 percent fewer casualties around Kandahar than it had during the same period in 2016.The documents credited the night-vision equipment for the marked reduction, concluding that the devices are "Becoming an integral part of base defense plans." The American military is now planning to equip the unit with roughly 2,500 night-vision goggles as part of what the documents described as a concept for a "Permanent program." Capt. Tom Gresback, a spokesman for United States forces in Afghanistan, declined to comment on the plans to distribute the devices to the Afghan National Army, as outlined in the military documents. With the night-vision devices, Taliban fighters have been able to approach Afghan bases nearly undetected before attacking. Zabihullah Mujahid, a spokesman for the Taliban, said fighters obtained night-vision devices after attacking Afghan bases or capturing members of the Afghan security forces. In Helmand Province, Marine Corps advisers are helping a request by the 505th Zone of the Afghan National Police to receive night-vision devices, Col.Reduced

我想得到以下输出(删除“[?”?]“和”减少“):

The Taliban Have Gone High-Tech. That Poses a Dilemma for the U.S. The frequency and ferocity of the nighttime Taliban attacks are linked to attempts by Afghan forces, based in small checkpoints across the country, to hold territory that has been wrested away from the militants. Afghan troops said the missing devices were reported as "Battle losses," but could not support that claim with any proof or records to explain where or when they were left behind, according to the documents. Over the summer and fall, the Afghan National Army suffered 15 percent fewer casualties around Kandahar than it had during the same period in 2016.The documents credited the night-vision equipment for the marked reduction, concluding that the devices are "Becoming an integral part of base defense plans." The American military is now planning to equip the unit with roughly 2,500 night-vision goggles as part of what the documents described as a concept for a "Permanent program." Capt. Tom Gresback, a spokesman for United States forces in Afghanistan, declined to comment on the plans to distribute the devices to the Afghan National Army, as outlined in the military documents. With the night-vision devices, Taliban fighters have been able to approach Afghan bases nearly undetected before attacking. Zabihullah Mujahid, a spokesman for the Taliban, said fighters obtained night-vision devices after attacking Afghan bases or capturing members of the Afghan security forces. In Helmand Province, Marine Corps advisers are helping a request by the 505th Zone of the Afghan National Police to receive night-vision devices, Col.

在python上,下面的regex成功地去掉了“[?]和“Reduced”:'\[\?]\s*(.*?)Reduced'。但是,在Javascript上,同样的regex返回:

?]The Taliban Have Gone High-Tech. That Poses a Dilemma for the U.S. The frequency and ferocity of the nighttime Taliban attacks are linked to attempts by Afghan forces, based in small checkpoints across the country, to hold territory that has been wrested away from the militants. Afghan troops said the missing devices were reported as "Battle losses," but could not support that claim with any proof or records to explain where or when they were left behind, according to the documents. Over the summer and fall, the Afghan National Army suffered 15 percent fewer casualties around Kandahar than it had during the same period in 2016.The documents credited the night-vision equipment for the marked reduction, concluding that the devices are "Becoming an integral part of base defense plans." The American military is now planning to equip the unit with roughly 2,500 night-vision goggles as part of what the documents described as a concept for a "Permanent program." Capt. Tom Gresback, a spokesman for United States forces in Afghanistan, declined to comment on the plans to distribute the devices to the Afghan National Army, as outlined in the military documents. With the night-vision devices, Taliban fighters have been able to approach Afghan bases nearly undetected before attacking. Zabihullah Mujahid, a spokesman for the Taliban, said fighters obtained night-vision devices after attacking Afghan bases or capturing members of the Afghan security forces. In Helmand Province, Marine Corps advisers are helping a request by the 505th Zone of the Afghan National Police to receive night-vision devices, Col.Reduced,]The Taliban Have Gone High-Tech. That Poses a Dilemma for the U.S. The frequency and ferocity of the nighttime Taliban attacks are linked to attempts by Afghan forces, based in small checkpoints across the country, to hold territory that has been wrested away from the militants. Afghan troops said the missing devices were reported as "Battle losses," but could not support that claim with any proof or records to explain where or when they were left behind, according to the documents. Over the summer and fall, the Afghan National Army suffered 15 percent fewer casualties around Kandahar than it had during the same period in 2016.The documents credited the night-vision equipment for the marked reduction, concluding that the devices are "Becoming an integral part of base defense plans." The American military is now planning to equip the unit with roughly 2,500 night-vision goggles as part of what the documents described as a concept for a "Permanent program." Capt. Tom Gresback, a spokesman for United States forces in Afghanistan, declined to comment on the plans to distribute the devices to the Afghan National Army, as outlined in the military documents. With the night-vision devices, Taliban fighters have been able to approach Afghan bases nearly undetected before attacking. Zabihullah Mujahid, a spokesman for the Taliban, said fighters obtained night-vision devices after attacking Afghan bases or capturing members of the Afghan security forces. In Helmand Province, Marine Corps advisers are helping a request by the 505th Zone of the Afghan National Police to receive night-vision devices, Col.

(未能完全剥离[?]). 你知道吗

我在python上测试了regex,它按预期工作:https://regex101.com/r/T16EcU/1/

然而,正如预期的那样,当我用Javascript测试它时,它未能去掉[?]:https://regex101.com/r/XKtn7s/1

如何修改regex表达式使其在javascript上工作?你知道吗

还值得注意的是,有时初始字符串在[?]而不是直接在[?]. 你知道吗

下面是我的Javascript代码:

//"documentBodyText.match" equals the sample string I gave in the post
const summary = documentBodyText.match('\[\?]\s*(.*?)Reduced');
console.log(summary);

下面是我的python代码:

//summary_page_text equals the sample string I gave in the post
target_quote_object = re.search('\[\?]\s*(.*?)Reduced', summary_page_text)
target_quote_text = target_quote_object.group(1)
print(target_quote_text)

Tags: ofthetoinforasaredocuments
1条回答
网友
1楼 · 发布于 2024-09-26 22:49:51

当您使用.match("\[\?]\s*(.*?)Reduced")时,正则表达式是用构造函数表示法构建的,并且单个反斜杠丢失,因此您的正则表达式实际上是[?]s*(.*?)Reduced,它匹配一个文字?,然后是零个或多个s字符,然后捕获到第一个Reduced之前的任何0+个字符。不是你想要的。你知道吗

您需要使用regex文本定义regex,并以与Python中相同的方式访问group 1:

var m = s.match(/\[\?]\s*(.*?)Reduced/); // Get the match
if (m)                                   // If there was a match
    console.log(m[1])                    // Grab what is in Group 1

请参见JS演示:

相关问题 更多 >

    热门问题