Fantoma Din Umbra
Fantoma Din Umbra

Reputation: 99

Regex matches which are not followed by a string

I am using the following regex for detecting negative numbers:

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

But I want to skip the matches which are followed by $. If i use the folowing regex:

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

It will match correctly the positions but will include the following character. For example in expression:

-0.6+3 - 3.0$

it will match:

-0.6+

I want to match only

-0.6

Upvotes: 5

Views: 71

Answers (3)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626806

You can use the regex from Regular-Expressions.info with just minus at the beginning, and a \b added at the end in order to stop before any non-word character:

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

This regex also captures the numbers with exponent part.

See demo

Upvotes: 0

Toto
Toto

Reputation: 91415

Remove the $ from the group:

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

You could use this simplified regex:

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

Upvotes: 0

vks
vks

Reputation: 67968

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

You need a negative lookahead here which will not consume and only make an assertion.

Upvotes: 6

Related Questions