Regular expression matches not followed by a string

I use the following regular expression to detect negative numbers:

([-]([0-9]*\.[0-9]+|[0-9]+)) 

But I want to skip the matches followed by $. If I use the following expression:

 ([-]([0-9]*\.[0-9]+|[0-9]+)[^\$]) 

It will match the positions correctly, but will contain the next character. For example, in the expression:

 -0.6+3 - 3.0$ 

It will match:

 -0.6+ 

I want to combine only

 -0.6 
+5
source share
3 answers
 ([-]([0-9]*\.[0-9]+|[0-9]+)(?!\$) 

You need a negative lookahead that will not consume and only make a statement.

+6
source

Remove $ from the group:

 ([-]([0-9]*\.[0-9]+|[0-9]+))[^\$] 

You can use this simplified regular expression:

 (-[0-9]+(?:\.[0-9]+)?)(?!\$) 
0
source

You can use the regular expression from Regular-Expressions.info with the minimum value at the beginning and \b added at the end to stop before any character other than the word:

 [-][0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\b 

This regular expression also captures numbers with an exponential part.

Watch the demo

0
source

All Articles