从特定单词开始提取字符串并以一年结束的弦

发布于 2025-01-23 03:57:46 字数 397 浏览 2 评论 0原文

输入1:字符串是封闭的。

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

输入2:弦是封闭式案例号:上诉(民事)6408 2007年之间。

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

我想提取以案例号(案例不敏感)的字符串,结束,这是一个数字的第二次出现。

我尝试了以下代码。

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

INPUT 1: The string is enclosed CASE NO.: Appeal (civil) 648 of 2007 in between.

OUTPUT 1: Appeal (civil) 648 of 2007

INPUT 2: The string is enclosed CASE NO.: Appeal (civil) 6408 of 2007 in between.

OUTPUT 2: Appeal (civil) 6408 of 2007

I want to extract the string starting with the word CASE NO.(Case Insensitive) and ending with the year being the second occurrence of a number.

I have tried the following code.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追风人 2025-01-30 03:57:46

我将在此处使用一个懒点来匹配案例之后的最近一年:

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

I would use a lazy dot here to match the nearest year occurring after 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
就是爱搞怪 2025-01-30 03:57:46
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())
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())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文