问题描述
这是正则表达式代码:
without_header = re.findall('/sports/[a-z0-9\/\.\-\:]*[0-9\.]+cms',without_header_url)
它返回给我每个URL的输出,这些URL的前面没有https头。 例如:
/sports/cricket/ipl/top-stories/kxip-vs-csk-shane-watson-faf-du-plessis-infuse-life-into-csks-ipl-campaign-shape-confidence-boosting-win-over-kxip/articleshow/78481088.cms'
/sports/football/epl/top-stories/epl-manchester-united-humiliated-as-mourinhos-spurs-win-6-1-at-old-trafford/articleshow/78481304.cms
为此,我想在开头添加“ https://example.com”。我不需要for循环,是否有使用re.sub进行此操作的有效方法?
解决方法
您可以在import com.shiva.truckerapi.entity.Readings;
global com.shiva.truckerapi.entity.Alert alert;
rule "EngineCoolantOrCheckEngineLight"
when
readingsObject: Readings(engineCoolantLow || checkEngineLightOn);
then
alert.setVin(readingsObject.getVin());
alert.setPriotity("LOW");
alert.setDescription("Engine Coolant LOW Or Check Engine Light ON");
alert.setTimestamp(readingsObject.getTimestamp());
alert.setLatitude(readingsObject.getLatitude());
alert.setLongitude(readingsObject.getLongitude());
System.out.println(alert.toString());
end;
中使用此正则表达式:
re.sub
代码:
(?<!:/)(/sports/[a-z0-9/.:-]*[0-9.]+cms)
RegEx详细信息:
-
s = re.sub(r'(?<!:/)(/sports/[a-z0-9/.:-]*[0-9.]+cms)',r'https://\1',s)
:否定性落后,断言我们之前的位置没有(?<!:/)
-
:/
:匹配您的文字并在第1组中捕获