[text] add 'rextr()'

This commit is contained in:
Mike Fährmann
2025-05-23 17:28:58 +02:00
parent e8b7d93b43
commit f3ed15573a
3 changed files with 36 additions and 4 deletions

View File

@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2022 Mike Fährmann
# Copyright 2015-2025 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
@@ -147,6 +147,15 @@ def rextract(txt, begin, end, pos=-1):
return None, pos
def rextr(txt, begin, end, pos=None, default=""):
"""Stripped-down version of 'rextract()'"""
try:
first = txt.rindex(begin, None, pos) + len(begin)
return txt[first:txt.index(end, first)]
except Exception:
return default
def extract_all(txt, rules, pos=0, values=None):
"""Calls extract for each rule and returns the result in a dict"""
if values is None: