在 Python 3 中,从命名组中提取文本非常容易,如下例所示:
import re
myStr = r"4000/2000/5000/7000"
reObj = re.compile(r"""(?P<g1>\d+) # a capturing group named g1
/
(?P<g2>\d+)
/
(?P<g3>\d+)
/
(?P<g4>\d+)""", re.VERBOSE)
matchObj = reObj.match(myStr) # match the string to be searched
print(matchObj.group("g1")) # 4000
print(matchObj.group("g2")) # 2000
print(matchObj.group("g3")) # 5000
print(matchObj.group("g4")) # 7000
Run Code Online (Sandbox Code Playgroud)
但是,在 LibreOffice Calc 中,我根本找不到任何线索(Calc 甚至没有独立的 regex() 函数来提供正则表达式模式)。这篇文章中基于位置的解决方法不是我需要的。
请给出独立于位置参数的答案,并请明确举例说明。例如 MID() 是不可接受的。虽然这里给出的例子已经足够简单,但我需要一种通用的方法来处理复杂得多的真实情况。