Nov-25-2025, 04:34 PM
(This post was last modified: Nov-25-2025, 04:34 PM by Moltar1997.)
I'm a novice at programming. One small-scale project I'm working on is an apt history log reader with regex designed to make the output easier. At some point I'll incorporate the output into a Tkinter UI.
The regex itself works, but I think the code could be better. The current code:
Major rule in programming: don't repeat yourself, right? But I'm searching for two different patterns and formatting one pattern in one way, and formatting another pattern in a different way.
I'm not at all familiar with Python's regex functionality and everything I've searched for simply confuses me.
The regex itself works, but I think the code could be better. The current code:
import re
file_path = "/var/log/apt/history.log" # Hardcoded as the filename and location never changes.
semicolon_regex = r'(: )'
comma_regex = r'(, )'
def text_replace(file_path = "/var/log/apt/history.log"):
with open(file_path, "r") as log:
text = log.read()
ntext = re.sub(comma_regex, "\n\t", text) # Replace comma + space with newline + tab
ptext = re.sub(semicolon_regex, ":\n\t", ntext) # Replace semicolon + space with semicolon + newline + tab.
print(ptext)
text_replace() # This will be a function call in a separate file later on.I notice that I have two separate variables, each for a different pattern:ntextfor replacing "comma + single blank space" with "newline + tab".
ptextfor replacing "semicolon + single blank space" with "semicolon + newline + tab".
Major rule in programming: don't repeat yourself, right? But I'm searching for two different patterns and formatting one pattern in one way, and formatting another pattern in a different way.
I'm not at all familiar with Python's regex functionality and everything I've searched for simply confuses me.
