0

I am searching for a string in a line using:

import re

myfile = "myfile.txt"
files = open(myfile, 'r').read().splitlines()
for line in file:
    if re.search("`this", line):
        print "bingo"

This works fine. However, I want to exclude any lines that are comments. The comments in the file that I am reading the lines from can have comments in the form of //. I'm not sure how to exclude the comments though. Comments might start anywhere in the line, not necessarily at the beginning of the line.

Example:

I want to exclude lines like first_last = "name" //`this THAT since "`this" is in a comment

  • Possible duplicate: https://stackoverflow.com/questions/51312345/regex-to-find-a-string-excluding-comments –  Apr 01 '20 at 04:37
  • python doesn't support `//` comment – Always Sunny Apr 01 '20 at 05:11
  • @AlwaysSunny My script is in python but the file I am reading is not. The file I am reading has comments as I mentioned. – programminglearner Apr 01 '20 at 05:13
  • @Mandy8055 I tested that on a python regex tester but that does not seem to work for python. Throws out errors. – programminglearner Apr 01 '20 at 05:15
  • @sfr You do not need to copy the `regex` as it is because that is written for .net language. Please read [**this**](https://stackoverflow.com/questions/4108542/using-a-net-regex-in-python) –  Apr 01 '20 at 05:18

1 Answers1

0

This can be done with a variable length negative lookbehind assertion, but for that you need to use the regex package installable with pip form the PyPi repository. The regex is:

(?<!//.*)    # negative lookahead assertion stating that the following must not be preceded by // followed by 0 or more arbitary characters
`this        # matches `this

The code:

import regex as re

regex = re.compile(r'(?<!//.*)`this')
myfile = "myfile.txt"
with open(myfile, 'r') as f:
    for line in f: # line has newline character at end; call rstrip method on line to get rid if you want
        if regex.search(line):
            print(line, end='')

Regex Demo

Booboo
  • 18,421
  • 2
  • 23
  • 40