php - 负前瞻与预期不匹配
问题描述
我正在尝试使用preg_replace()
. 我需要检查这个词后面是否没有任何这个 set [a-zA-Z0-9_$.]
。为了实现这一点,我使用了否定的lookahead,例如,如果目标词是"foo"
并且我有一个字符串,例如:
"hello_foo this foo is an example.foo"
...我想foo
用bar
这个正则表达式替换
$old = "hello_foo this foo is an example.foo";
$new = preg_replace("/(?![a-zA-Z0-9_$.])foo/", "bar" $old);
...这应该返回
"hello_foo this bar is an example.foo"
这没有按预期工作。但是,如果我a-z
从正则表达式中删除该集合,它确实可以工作,但现在它会将这样的单词替换"aaafoo"
为"aaabar"
.
我在这里错过了什么吗?我是正则表达式的新手。
解决方案
推荐阅读
- java - Adobe AEM/OSGI:我应该将我的 OSGI 配置 (org.apache.sling.security.impl.ReferrerFilter) 放在哪里,以便作者和出版商都能使用它?
- css - html正文在tailwindcss中没有得到全宽
- mysql - 如何优化更好的mysql查询?
- mysql - 在 HTML 中使用时未定义 JSON 数据
- javascript - axios 请求的问题
- javascript - React Router 5 - 将 POP 处理为路由的硬重载?
- python - 如何在 python 中模拟网络摄像头
- swift - 如何在 Swift 中添加 Admob 非奖励视频广告
- php - 为什么来自 endroid 的 QR 码在 codeigniter 4 中无法正确显示
- css - ReactJS - 如何添加/使用引导类?