regex - 正则表达式替换:替换文本,而不是代码
问题描述
我试图解决几天的正则表达式测验,但仍然无法正确解决。我已经很近了,但仍然无法通过。
任务:
在 HTML 页面中,将文本替换
micro
为µ
. 哦,不要搞砸代码:不要更换内部<the tags>
或&entities;
代替
micro
->µ
abc micro
->abc µ
micromicro
->µµ
µmicro
->µµ
不要触摸
<tag micro />
-><tag micro />
µ
->µ
&abcmicro123;
->&abcmicro123;
我试过这个,但最后失败了µ
,我错过了什么?有人能指出我错过了什么吗?提前致谢!
我试过的:
正则表达式
((?:\G|\n)(?:.*?&.*?micro.*?;[\s\S]*?|.*?<.*?micro.*?>[\s\S]*?|.)*?)micro
替代
$1µ
解决方案
使用SKIP-FAIL 技术,但作为一个整体匹配:
(?:<[^<>]*>|&\w+;)(*SKIP)(*F)|\bmicro\b
见证明
解释
--------------------------------------------------------------------------------
(?: group, but do not capture:
--------------------------------------------------------------------------------
< '<'
--------------------------------------------------------------------------------
[^<>]* any character except: '<', '>' (0 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
> '>'
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
& '&'
--------------------------------------------------------------------------------
\w+ word characters (a-z, A-Z, 0-9, _) (1 or
more times (matching the most amount
possible))
--------------------------------------------------------------------------------
; ';'
--------------------------------------------------------------------------------
) end of grouping
--------------------------------------------------------------------------------
(*SKIP)(*F) Skip the match and go on matching from current location
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
--------------------------------------------------------------------------------
micro 'micro'
--------------------------------------------------------------------------------
\b the boundary between a word char (\w) and
something that is not a word char
推荐阅读
- redux - 如何刷新模拟 fn 调用?
- python-3.x - 主题建模输出的可视化
- php - 无法生成报告
- javascript - 使用reduce方法对javascript循环中的所有值求和不起作用
- java - Criteria Query, JPA 2 - 使用谓词构建带有 IN 子句的查询
- css - 引导程序无法在移动设备上运行(Python 烧瓶)
- reactjs - React Native 中的图像导入过多导致白屏
- python - 根据随机名称更新特定的 csv 列
- c++ - C++ 向量:clear() 和 resize() 之间的区别
- android - Kotlin 中未解决的参考 WearableRecyclerView.Adapter 错误