Open miklschmidt opened 3 weeks ago
What's the context for this regex?
Specifically,
If 3, then a python script that reads the conf file and properly parses it could be a better solution.
Sample code would look something like this:
#! /usr/bin/env python3
import re
import configparser
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-r", "--repo", dest="repo")
def extract_commit(repo, file_name):
section_name = "update_manager" + " " + repo
config = configparser.ConfigParser()
config.read(file_name)
if section_name not in config.sections():
return None
if "pinned_commit" not in config[section_name]:
return None
return config[section_name]["pinned_commit"]
if __name__ == "__main__":
(opts, args) = parser.parse_args()
pin = extract_commit(opts.repo, args[0])
if pin is None:
exit(1)
print(pin)
Result:
$ /tmp/regex.py -r KlipperScreen moonraker.conf
71eef9ee1f23aa4fd6b68169cfe5dd7908e478b2
or
$ /tmp/regex.py -r foo moonraker.conf
$ echo $?
1
The following somewhat janky regex should work