首页 > 解决方案 > Remove "." (dot) after each word enclosed in pattern

问题描述

I want to remove "." (dot) after each word enclosed in a pattern.

Input :

Lorem *|Ipsum.|* is simply *|dummy.|* text of the *|printing|* and typesetting *|industry.|*.

Output :

Lorem *|Ipsum|* is simply *|dummy|* text of the *|printing|* and typesetting *|industry|*.

标签: phpregexpreg-replaceregex-lookaroundsregex-group

解决方案


您可以使用此正则表达式捕获*|sometext.|*模式文本,并进行适当的分组,

(\*\|[^|]+)\.(\|\*)

并替换为在 group1 中捕获部分并$1$2从分组中排除的位置,以便将其删除并将部分分组到 group2 中。*|sometext.|*

正则表达式演示

Python代码演示

$s = "Lorem *|Ipsum.|* is simply *|dummy.|* text of the *|printing|* and typesetting *|industry.|*.";
echo preg_replace('/(\*\|[^|]+)\.(\|\*)/', '$1$2', $s);

打印以下,去除了内部的点*|text.|*

Lorem *|Ipsum|* is simply *|dummy|* text of the *|printing|* and typesetting *|industry|*.

推荐阅读