替换字符串中的冒号,除非它们是不合格子字符串的一部分
我想将 :
替换为 \xEF\xBC\x9A
但是,我不想对 之后的
、:
执行此操作httphttps
以及格式为 @[{numbers}:{numbers}:{some text}]
的字符串。 我所拥有的并没有真正起作用,因为 (? 不会检查其后面的内容。我当前的实现仅适用于
@[1:2:text]
之类的内容。
$string = preg_replace(
'#(?<!https)(?<!http)(?<!@\[\d)(?<!@\[\d:\d):#',
"\xEF\xBC\x9A",
$string
);
I would like to replace :
with \xEF\xBC\x9A
however, I do not want to do this for :
that follows http
, https
, and string with the format of @[{numbers}:{numbers}:{some text}]
.
What I have doesn't really work as (?<!@\[\d)
does not check what's after it. My current implementation only work for something like @[1:2:text]
.
$string = preg_replace(
'#(?<!https)(?<!http)(?<!@\[\d)(?<!@\[\d:\d):#',
"\xEF\xBC\x9A",
$string
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
Try this:
试试这个正则表达式:
它应该与此处的第一个和第二个“:”实例匹配。
使用示例:
Try this regex:
It should match the first and second instances of ':' here.
Usage Sample:
匹配不应改变的表达式,然后使用
(*SKIP)(*FAIL)
取消它们的资格——这比尝试避免匹配它们要简单得多。然后匹配任何非不合格的冒号并将其替换为所需的字符串。代码:(演示)
输出:
Match the expressions that should not be mutated then disqualify them with
(*SKIP)(*FAIL)
-- this is far simpler than trying to avoid matching them. Then match any non-disqualified colon and replace it with the desired string.Code: (Demo)
Output: