Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative look ahead java

Tags:

java

regex

I need an expression to capture a string like this:

"A"[A string that is NOT atleast 5 and atmost 6 digits]"B", In other words capture anything that is NOT the following

A[0-9][0-9][0-9][0-9][0-9]B
A[0-9][0-9][0-9][0-9][0-9][0-9]B

I have tried the negative look ahead

regex = "a((?![0-9]{5,6}).)*d" ;

But it fails to capture all scenarios.

like image 970
venu Avatar asked Dec 10 '22 16:12

venu


1 Answers

A(?!\d{5,6}B).*B

You only want to do the lookahead once, right after the A. And you have to include the B in the lookahead so it doesn't reject anything with more than six digits.

like image 98
Alan Moore Avatar answered Dec 15 '22 00:12

Alan Moore