Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex matches which are not followed by a string

Tags:

regex

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
like image 360
Fantoma Din Umbra Avatar asked May 04 '15 12:05

Fantoma Din Umbra


People also ask

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

Does regex work only with strings?

So, yes, regular expressions really only apply to strings. If you want a more complicated FSM, then it's possible to write one, but not using your local regex engine. Save this answer.

Which character does does not match in single line mode of regex?

\N Never Matches Line Breaks Perl 5.12 and PCRE 8.10 introduced \N which matches any single character that is not a line break, just like the dot does. Unlike the dot, \N is not affected by “single-line mode”.


1 Answers

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

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

like image 154
vks Avatar answered Oct 06 '22 19:10

vks