用于提取以特定单词开头并以年份结尾的字符串的正则表达式

Regex for a extracting a string starting with a particular word and ending with a year

INPUT 1: 字符串被附上 CASE NO.: Appeal (civil) 648 of 2007 in between.

输出 1: 2007 年上诉(民事)648

INPUT 2: 字符串被附上 CASE NO.: Appeal (civil) 6408 of 2007 in between.

输出 2: 2007 年上诉(民事)6408

我想提取以单词 CASE NO.(不区分大小写)开始并以年份结束的字符串。

我试过下面的代码。

case_no = re.search(r'(?=Case No)(\w+\W+)*?\b\d{4}\b', contents, re.IGNORECASE)
    if case_no:
        print(case_no.group(0))

我会在这里使用惰性点来匹配 CASE NO. 之后最近的年份:

inp = "The string is enclosed CASE NO.: Appeal (civil) 6408 of 2007 in between."
m = re.search(r'\bCASE NO\.:\s*(.*\b\d{4}\b)', inp)
print(m.group())  # Appeal (civil) 6408 of 2007
inp = "The string is enclosed CASE NO.: Appeal (civil) 6408 of 2007 in between."
case_no = re.search(r'(?=Case No)(\w+\W+)*?\d+(\w+\W+)*?\b\d{4}\b', inp, re.IGNORECASE)
print(case_no.group())