Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Match numbers not preceded by string

Tags:

python

regex

I'm analysing some very large log files using Python regex. I need to substitute every number in the log file, except the numbers that are preceded by 'java:' (the log files are made by a java program).

This means that given we have a line saying:

This is a bogus test line with limit=300 doing 53 rounds and the error is in (Abc.java:417) and some more

The numbers 300 and 53 should be replaced, but not 417.

I filter on a line basis, and it should be noted that not all lines contain java:[number].

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

like image 295
beruic Avatar asked Oct 29 '13 09:10

beruic


1 Answers

Probably what's happening with

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

is that, sure, the match at this point,

java:
     ^

fails, but then at _this point,

java:4
      ^

succeeds, because indeed, ava:4 is not java:.

You'll just need to add one more negative lookbehind,

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

so that only "complete" numbers are considered.

like image 58
slackwing Avatar answered Sep 26 '22 10:09

slackwing