-2

I would like to know how to create a RegEx with Python for the following issue:

I got a textfile with data in it, the regex should only match if:

  • it has the right ID
  • and does not include a specific value2 or value3 within the textfile (which can occur anywhere)

Textfile:

blabla
ID
blabla
...
blabla
value1
blabla
...
blabla
value2
blabla
...

This

(?<!\n)(\n.*(ID)(?!\n.*(value2|value3).*)

works, but only if value2 or value3 is on the line before or right after the line ID.

So, how to look up any line before and after the line ID? The solution should fit a single line of code like my try above.

J.Doe
  • 3
  • 2

2 Answers2

0

Do change your title as it's misleading, "Retrieving ID from Text File".

Possible duplication Python - search txt.file for ID, then return variable from line below

Your text file should have a standard structure or format when writing the records. Explanation by @Blender in Writing a dict to txt file and reading it back or using dictionary format could help simplify in recognize the id and value next time when reading the file.

Nicely formatted text file, using loop then read every new line then perform split and into dict.

Note: Wanted to write in comment but not enough reputation

ncode
  • 45
  • 7
  • Thank you, but the solution should fit a single line of code like my try above without any python code – J.Doe Jul 26 '18 at 10:46
  • the value2 and value3 can varies from a wide range of values right? if not you just cant filter off value2 and value3 in your regex. Please clear my understanding. – ncode Jul 26 '18 at 11:03
  • I know the values of ID, value2 and value3. val2 and val3 should trigger no match for the RegEx, if they occur in the file. – J.Doe Jul 26 '18 at 12:52
0

I believe

^(?![\s\S]*(value2|value3))[\s\S]*(ID)[\s\S]*

should work for what you want to do.

The Python documentation on Regular expression operations includes more detailed info on \s, which matches whitespace characters and \S, which matches non-whitespace characters.

There's lots more info on negative lookahead – the (?!...) bit in the beginning – in this Stack Overflow post: Regex lookahead, lookbehind and atomic groups

Kay
  • 797
  • 1
  • 6
  • 28