Griff
Griff

Reputation: 1747

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.

Upvotes: 1

Views: 310

Answers (1)

nhahtdh
nhahtdh

Reputation: 56819

You can do this:

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

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

Upvotes: 3

Related Questions