0

I'm trying to produce the following "Query ID: XYZABC"

My code is as follows from a .txt file that has one instance of the following "Query= xyxysdfl;jas"

#!/usr/bin/env python3

import re

for line in open("/home/example.txt"):
    query_id = re.search(r"Query=(.*$)", line)
    if query_id != None:
        print("Query ID: " + query_id.group())

This produces the following:

Query ID: Query= xyslkjjdf

How can I eliminate the "Query="? I thought I was doing that by including the (.*$)?

Matt
  • 17
  • 1
  • 5

1 Answers1

0

You need to get the first captured group:

query_id.group(1)

without any explicit group reference the 0-th group i.e. the whole match is produced.


Notes:

  • None is a singleton in Python, so do the identity check by is:

    if query_id is not None:
    

    Or better as None is falsey, you can just do the truthy check:

    if query_id:
    
  • It's not strictly needed, but for readability you should leave the end of line specifier, $, out of the captured group:

    (.*)$
    

Taking the notes into account, the final form could be:

query_id = re.search(r"Query=(.*)$", line)
if query_id:
    print("Query ID: " + query_id.group(1))
heemayl
  • 39,294
  • 7
  • 70
  • 76