简体   繁体   English

PHP正则表达式向前看并向后看 - 两者都不匹配?

[英]PHP Regex look ahead and look behind - both must not match?

I am currently looking for a way to remove fullstops from a string in certain places. 我目前正在寻找一种方法来删除某些地方的字符串中的fullstops。

I want it so that it will remove fullstops only if 2 conditions are not met; 我希望它只有在不满足2个条件时才会删除fullstops;

There is not a digit before the fullstop. 在fullstop之前没有数字。

AND

There is not a digit after the fullstop. 在fullstop之后没有数字。

I currently have this regex 我目前有这个正则表达式

'#(?<!\d)\.(?!\d)#'

But this does not remove fullstops in strings such as 但这并没有删除字符串中的fullstops,例如

'hello.1', '1.hello'

I am guessing that as there is either a digit before or after the fullstop the match fails and it is not recognized. 我猜测,因为在完全停止之前或之后有一个数字,匹配失败并且无法识别。

How can I make it so that both the look ahead and look behind must be met in order for there to be a match and the fullstop get removed correctly? 我怎样才能做到这一点,以便必须满足前方和后方,以便匹配并正确删除fullstop?

Thank you. 谢谢。

EDIT 编辑

I want it to remove fullstops when and only when there is not a digit before ~OR~ there is not a digit after the fullstop. 我希望它删除fullstops当且仅当没有数字之前〜或〜在fullstop之后没有数字。 So there can only be a fullstop if the string is like so (a digit).(a digit) but will remove fullstops in any other circumstances. 因此,如果字符串是这样的(a digit).(a digit)那么只能有一个fullstop,但在任何其他情况下都会删除fullstops。

You can do this: 你可以这样做:

'#((?<!\d)\.|\.(?!\d))#'

It will remove the dot if it is preceded or succeeded by non-digit. 如果点前面或后面有非数字,它将删除点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM