How to verify certain strings do not appear at certain spots in a line of text

26 Views Asked by At

I want to verify that “0000000” does not occur at a certain position and also that “000” does not occur at a subsequent position. Any non-zero set of numbers is acceptable.

This should fail because the “0”s appear in both places where they should not:

Case 1: 77777770000000333000

These also should fail because one or the other slot has “0”s where they should not:

Case 2: 77777770001000333000

Case 3: 77777770000000333010

This should not fail because neither slot is all “0”s:

CASE 4: 77777770000100333010

I have tried: .{7}(?!0{7}).{3}(?!0{3})

I want it to only match case 4 but it is only matching case 2.

1

There are 1 best solutions below

0
jwrey01 On

Solved! The answer is - .{7}(?!0{7}).{7}.{3}(?!0{3}).{3}

The reason lies in negative lookahead not consuming any data. It looks ahead to prove the data does not match the pattern but doesn’t move the “cursor” beyond that data.

So for my original pattern: .{7}(?!0{7}).{3}(?!0{3}) and Case 4 data: 77777770000100333010

7777777 is consumed by .{7} and the “cursor” is at 0000100333010.

The next 7 digits 0000100 pass the negative lookahead (?!0{7}) test but are not consumed. The cursor is still at 0000100333010.

000 is consumed by .{3} and the cursor is at 0100333010.

010 passes the (?!0{3}) test. The cursor is at 0100333010.

So my solution is to follow the negative lookahead with a consuming pattern.

Is this the best technique? It is only intuitive after I grasped what it meant for negative lookahead to not consume anything.