Match numbers not preceded by a string

I parse very large log files using Python regex. I need to replace every number in the log file, with the exception of numbers preceded by "java:" (log files are created using a java program).

This means that if we have a line saying:

This is a dummy test line with a constraint = 300, performing 53 rounds, and the error is in (Abc.java:417) and a few more

Numbers 300 and 53 should be replaced, but not 417.

I am filtering based on the string, and it should be noted that not all rows contain java:[number] .

The closest I got is ((?<!java:)[0-9]+)

+6
source share
1 answer

Possible what happens to

 ((?<!java:)[0-9]+) 

- this, of course, is a coincidence at this point,

 java: ^ 

fails but then at this point

 java:4 ^ 

succeeds, because indeed, ava:4 not java:

You just need to add another negative lookbehind,

 ((?<!java:)(?<![0-9])[0-9]+) ^^^^^^^^^^ 

so only "full" numbers are taken into account.

+4
source

Source: https://habr.com/ru/post/956930/


All Articles