如何在正则表达式匹配的开头添加一些内容?

问题描述

这是正则表达式代码

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

RegEx Demo

代码:

(?<!:/)(/sports/[a-z0-9/.:-]*[0-9.]+cms)

RegEx详细信息:

  • s = re.sub(r'(?<!:/)(/sports/[a-z0-9/.:-]*[0-9.]+cms)',r'https://\1',s) :否定性落后,断言我们之前的位置没有(?<!:/)
  • :/:匹配您的文字并在第1组中捕获

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...