简体   繁体   中英

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.

I want it so that it will remove fullstops only if 2 conditions are not met;

There is not a digit before the fullstop.

AND

There is not a digit after the fullstop.

I currently have this regex

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

But this does not remove fullstops in strings such as

'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?

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. 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.

You can do this:

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

It will remove the dot if it is preceded or succeeded by non-digit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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