[英]PHP particular regular expression
I am trying to target only digits that follows non-numeric characters in a string to add some html tags around them. 我试图只针对字符串中非数字字符后面的数字来添加一些html标签。 For the moment i am able to do it on every digit like this :
目前我能够在每个数字上执行此操作:
$string = preg_replace('/(\d+)/','<sub>\1</sub>', $string );
How can i do to just target digits that follows non-numeric characters ? 如何定位非数字字符后面的数字呢? The goal is to format strings containing chemical formulas but not the text around it.
目标是格式化包含化学公式但不包含其周围文本的字符串。
Actually 20% O2 in N2
becomes <sub>2</sub><sub>0</sub>% O<sub>2</sub> in N<sub>2</sub>
实际上
20% O2 in N2
<sub>2</sub><sub>0</sub>% O<sub>2</sub> in N<sub>2</sub>
变为<sub>2</sub><sub>0</sub>% O<sub>2</sub> in N<sub>2</sub>
I would like it to be 20% O<sub>2</sub> in N<sub>2</sub>
instead. 我希望它
20% O<sub>2</sub> in N<sub>2</sub>
是20% O<sub>2</sub> in N<sub>2</sub>
。
How can i do this? 我怎样才能做到这一点? Is it possible?
可能吗?
Thank you very much for your help 非常感谢您的帮助
How can i do to just target digits that follows non-numeric characters ?
如何定位非数字字符后面的数字呢?
For digits that follow letters try with a lookbehind . 对于跟随字母的数字,请尝试使用lookbehind 。
(?<=[A-Za-z])\d+
See demo at regex101 (and replace with <sub>\\0</sub>
) 请参阅regex101上的演示 (并替换为
<sub>\\0</sub>
)
For unicode letters use \\pL
instead of [A-Za-z]
together with u
flag . 对于unicode字母,请使用
\\pL
而不是[A-Za-z]
和u
flag 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.