Regex: Lookahead and lookbehind, checking a . (dot) for use as decimal vs full-stop

I've had a good look around the net for an answer to this, but can't seem to get it working.

I have developed the following regex:

    (?<![^\d][\\])[\.](?![\d])

The objective is to identify any '.' (dots) that have not been escaped or that are part of a decimal number.

ie)

  • abc.co.uk, both dots should match
  • ab0.co.uk, both dots should match
  • abc.0.uk, both dots should match
  • abc\.co.uk, only the second dot gets matched
  • 0.00, dot should NOT match
  • abc0.0.uk, first dot would NOT match (which is an acceptable outcome), second dot should

At moment it works for all the cases above, except:

  • abc.0.uk, both dots should match

Any thoughts? It seems the look-behind is working correctly, however the look-ahead is not.

Am sure it'll be an easy one for any Regex gurus!

FYI. I'm developing this in .net 4

Answers


Try this one

(?<![\\\d])\.(?=\d)|(?<=[^\D\\])\.(?!\d)|(?<=[^\d\\])\.(?!\d)

See it here on Regexr

I broke it down in three steps.

  1. Match if before is not a escape character and not a digit and behind is a digit.

  2. Match if before is not a escape character and a digit and behind is not a digit

  3. Match if before is not a escape character and not a digit and behind is not a digit


Need Your Help

Error “unterminated string literal” in jQuery plugin

jquery

I've got a very strange error, been trying for ages to find it out in this file:

How do I store results of a computation in Python so my program doesn't compute the same thing twice?

python math memory runtime primes

I keep having problems with programs in Python (I'm a complete newb) where it doesn't store the data from a computation and does it over and over again when I feel like it should have saved it. How...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.