1

我正在使用正则表达式re.findall(r"[0-9]+(.*?)\.\s(.*?)[0-9]+", text)来获取以下文本

8    EXT./INT. MONORAIL - MORNING 8
9    EXT. CITY SCAPE/MONORAIL - CONTINUOUS 9

但是我当前的输出没有前缀和后缀数字。我试图在输出中也有前缀数字,如下所示。

9    EXT. CITY SCAPE/MONORAIL - CONTINUOUS 

非常感谢任何帮助!提前致谢。

(电流输出如下)

电流输出

4

1 回答 1

1

You can use

(?m)^([0-9]+)\s*(.*?)\.\s(.*?)(?:\s*([0-9]+))?$

See the regex demo. *Details:

  • (?m) - a multiline modifier
  • ^ - start of string
  • ([0-9]+) - Group 1: one or more digits
  • \s* - zero or more whitespaces
  • (.*?) - Group 2: zero or more chars other than line break chars as few as possible
  • \.\s - a dot and a whitespace
  • (.*?) - Group 3: zero or more chars other than line break chars as few as possible
  • (?:\s*([0-9]+))? - an optional occurrence of zero or more whitespaces and then Group 4 capturing one or more digits
  • $ - end of line.
于 2021-07-31T21:32:50.097 回答