在新行之后捕获可选内容

问题描述 投票:0回答:1

以下代码用于检索有关

LaTeX
文件中的包导入的信息。我没能抓住方括号中的可选日期。我该怎么做?

import re

test_str = r"""
\RequirePackage[
  top            = 2.5cm,
  bottom         = 2.5cm,
  left           = 2.5cm,
  right          = 2.5cm,
  marginparwidth = 2cm,
  marginparsep   = 2mm,
  heightrounded
]{geometry}%
 [2020-01-02]

\RequirePackage{tocbasic}

\RequirePackage[svgnames]%
               {xcolor}%
               [2023/11/15]

\RequirePackage[raggedright]%  OK?
                {titlesec}

\RequirePackage{xcolor}%
               [2022/06/12]

\RequirePackage{hyperref}% To load after titlesec!
               [2023-02-07]
    """

pattern = re.compile(
    r"\\RequirePackage(\[(.*?)\])?([^{]*?)?{(.*?)}",
    flags = re.S
)

matches = pattern.finditer(test_str)

for m in matches:
    print('---')

    for i in [0, 2, 4]:
        print(f"m.group({i}):")
        print(m.group(i))
        print()
python-3.x regex
1个回答
0
投票

我已将

?\[(.*?)\]
添加到您的正则表达式中,因此最终结果是:

r"\\RequirePackage(\[(.*?)\])?([^{]*?)?{(.*?)}?\[(.*?)\]"

但是,它位于第一和第六匹配组中。 (0 和 5)。不知道你是否需要0.

import re

test_str = r"""
\RequirePackage[
  top            = 2.5cm,
  bottom         = 2.5cm,
  left           = 2.5cm,
  right          = 2.5cm,
  marginparwidth = 2cm,
  marginparsep   = 2mm,
  heightrounded
]{geometry}%
 [2020-01-02]

\RequirePackage{tocbasic}

\RequirePackage[svgnames]%
               {xcolor}%
               [2023/11/15]

\RequirePackage[raggedright]%  OK?
                {titlesec}

\RequirePackage{xcolor}%
               [2022/06/12]

\RequirePackage{hyperref}% To load after titlesec!
               [2023-02-07]
    """

pattern = re.compile(
    r"\\RequirePackage(\[(.*?)\])?([^{]*?)?{(.*?)}?\[(.*?)\]", # edited this line
    flags = re.S
)

matches = pattern.finditer(test_str)

for m in matches:
    print('---')

    for i in [0, 2, 4, 5]: # Added 5
        print(f"m.group({i}):")
        print(m.group(i))
        print()

这是输出:

---
m.group(0):
\RequirePackage[
  top            = 2.5cm,
  bottom         = 2.5cm,
  left           = 2.5cm,
  right          = 2.5cm,
  marginparwidth = 2cm,
  marginparsep   = 2mm,
  heightrounded
]{geometry}%
 [2020-01-02]

m.group(2):

  top            = 2.5cm,
  bottom         = 2.5cm,
  left           = 2.5cm,
  right          = 2.5cm,
  marginparwidth = 2cm,
  marginparsep   = 2mm,
  heightrounded


m.group(4):
geometry}%
 

m.group(5):
2020-01-02

---
m.group(0):
\RequirePackage{tocbasic}

\RequirePackage[svgnames]

m.group(2):
None

m.group(4):
tocbasic}

\RequirePackage

m.group(5):
svgnames

---
m.group(0):
\RequirePackage[raggedright]%  OK?
                {titlesec}

\RequirePackage{xcolor}%
               [2022/06/12]

m.group(2):
raggedright

m.group(4):
titlesec}

\RequirePackage{xcolor}%
               

m.group(5):
2022/06/12

---
m.group(0):
\RequirePackage{hyperref}% To load after titlesec!
               [2023-02-07]

m.group(2):
None

m.group(4):
hyperref}% To load after titlesec!
               

m.group(5):
2023-02-07

© www.soinside.com 2019 - 2024. All rights reserved.