使用正则表达式匹配两个字符串之间的文本

2025-01-22 08:45:00
admin
原创
92
摘要:问题描述:我想使用正则表达式来匹配两个字符串之间的任意文本:Part 1. Part 2. Part 3 then more text 在这个例子中,我想搜索“第 1 部分”和“第 3 部分”,然后得到介于两者之间的所有内容:“第 2 部分。”我正在使用 Python 2x。解决方案 1:使用re.searc...

问题描述:

我想使用正则表达式来匹配两个字符串之间的任意文本:

Part 1. Part 2. Part 3 then more text

在这个例子中,我想搜索“第 1 部分”和“第 3 部分”,然后得到介于两者之间的所有内容:“第 2 部分。”

我正在使用 Python 2x。


解决方案 1:

使用re.search

>>> import re
>>> s = 'Part 1. Part 2. Part 3 then more text'
>>> re.search(r'Part 1.(.*?)Part 3', s).group(1)
' Part 2. '
>>> re.search(r'Part 1(.*?)Part 3', s).group(1)
'. Part 2. '

re.findall或者,如果出现多次,则使用。

解决方案 2:

使用正则表达式:

>>> import re
>>> s = 'Part 1. Part 2. Part 3 then more text'
>>> re.search(r'Part 1(.*?)Part 3', s).group(1)
'. Part 2. '

如果没有正则表达式,这个适用于您的示例:

>>> s = 'Part 1. Part 2. Part 3 then more text'
>>> a, b = s.find('Part 1'), s.find('Part 3')
>>> s[a+6:b]
'. Part 2. '

解决方案 3:

我觉得这些复杂程度不断增加的例子很有趣:

ppt = 'abc HERE abc'
ept = 'abc TERM abc'
re_ppt = ppt.replace('HERE', '(.+)')
print()
print(f'{re_ppt=}')
out = re.search(pattern=re_ppt, string=ept)
print(out)
print(out.groups())

ppt = 'abc HERE abc HERE abc'
ept = 'abc TERM1 abc TERM2 abc'
re_ppt = ppt.replace('HERE', '(.+)')
print()
print(f'{re_ppt=}')
out = re.search(pattern=re_ppt, string=ept)
print(out)
print(out.groups())

print()
ppt = """(fun n : nat =>
 nat_ind (fun n0 : nat => n0 + 0 = n0) ?Goal"""
print(f"{ppt=}")
ept = """(fun n : nat =>
 nat_ind (fun n0 : nat => n0 + 0 = n0) (eq_refl : 0 + 0 = 0)"""
print(f'{ept=}')
pattern_meta_var = r'?(w)+'
_ppt = re.sub(pattern=pattern_meta_var, repl='HERE', string=ppt)
print(f'{ppt=}')
_ppt = re.escape(_ppt)
print(f'{ppt=}')
re_ppt = _ppt.replace('HERE', '(.+)')
print(f'{re_ppt=}')
out = re.search(pattern=re_ppt, string=ept)
print(out)
print(out.groups())

print()

# sometimes the actual proof term missing won't have white spaces surrounding it but the ppt will have surrounding spaces where the hole
# would be. So in goal cames I removed the surrounding whitespaces. Then inserted a regex that accepts a hole with or
# without surrounding white spaces. That way in case the proof term in the hole does have surrounding white spaces then
# the regex hole catcher would match it anyway.
ppt = """
   (fun (n' : nat) (IH : n' + 0 = n') => ?Goal0) n)"""
ept = """
   (fun (n' : nat) (IH : n' + 0 = n') =>
    eq_ind_r (fun n0 : nat => S n0 = S n') eq_refl IH : S n' + 0 = S n') n)"""
print(f"{ppt=}")
print(f'{ept=}')
pattern_meta_var = r's*?(w)+s*'
_ppt = re.sub(pattern=pattern_meta_var, repl='HERE', string=ppt)
print(f'{_ppt=}')
_ppt = re.escape(_ppt)
print(f'{_ppt=}')
re_ppt = _ppt.replace('HERE', 's*(.+)s*')
print(f'{re_ppt=}')
out = re.search(pattern=re_ppt, string=ept)
print(out)
assert out is not None, f'expected two holes matched but go {out=}'
print(out.groups())

print()
ppt = """(fun n : nat =>
 nat_ind (fun n0 : nat => n0 + 0 = n0) ?Goal
   (fun (n' : nat) (IH : n' + 0 = n') => ?Goal0) n)"""
print(f"{ppt=}")
ept = """(fun n : nat =>
 nat_ind (fun n0 : nat => n0 + 0 = n0) (eq_refl : 0 + 0 = 0)
   (fun (n' : nat) (IH : n' + 0 = n') =>
    eq_ind_r (fun n0 : nat => S n0 = S n') eq_refl IH : S n' + 0 = S n') n)"""
print(f'{ept=}')
pattern_meta_var = r's*?(w)+s*'
_ppt = re.sub(pattern=pattern_meta_var, repl='HERE', string=ppt)
print(f'{_ppt=}')
_ppt = re.escape(_ppt)
print(f'{_ppt=}')
re_ppt = _ppt.replace('HERE', 's*(.+)s*')
print(f'{re_ppt=}')
out = re.search(pattern=re_ppt, string=ept)
print(out)
print(out.groups())
相关推荐
  政府信创国产化的10大政策解读一、信创国产化的背景与意义信创国产化,即信息技术应用创新国产化,是当前中国信息技术领域的一个重要发展方向。其核心在于通过自主研发和创新,实现信息技术应用的自主可控,减少对外部技术的依赖,并规避潜在的技术制裁和风险。随着全球信息技术竞争的加剧,以及某些国家对中国在科技领域的打压,信创国产化显...
工程项目管理   1565  
  为什么项目管理通常仍然耗时且低效?您是否还在反复更新电子表格、淹没在便利贴中并参加每周更新会议?这确实是耗费时间和精力。借助软件工具的帮助,您可以一目了然地全面了解您的项目。如今,国内外有足够多优秀的项目管理软件可以帮助您掌控每个项目。什么是项目管理软件?项目管理软件是广泛行业用于项目规划、资源分配和调度的软件。它使项...
项目管理软件   1354  
  信创国产芯片作为信息技术创新的核心领域,对于推动国家自主可控生态建设具有至关重要的意义。在全球科技竞争日益激烈的背景下,实现信息技术的自主可控,摆脱对国外技术的依赖,已成为保障国家信息安全和产业可持续发展的关键。国产芯片作为信创产业的基石,其发展水平直接影响着整个信创生态的构建与完善。通过不断提升国产芯片的技术实力、产...
国产信创系统   21  
  信创生态建设旨在实现信息技术领域的自主创新和安全可控,涵盖了从硬件到软件的全产业链。随着数字化转型的加速,信创生态建设的重要性日益凸显,它不仅关乎国家的信息安全,更是推动产业升级和经济高质量发展的关键力量。然而,在推进信创生态建设的过程中,面临着诸多复杂且严峻的挑战,需要深入剖析并寻找切实可行的解决方案。技术创新难题技...
信创操作系统   27  
  信创产业作为国家信息技术创新发展的重要领域,对于保障国家信息安全、推动产业升级具有关键意义。而国产芯片作为信创产业的核心基石,其研发进展备受关注。在信创国产芯片的研发征程中,面临着诸多复杂且艰巨的难点,这些难点犹如一道道关卡,阻碍着国产芯片的快速发展。然而,科研人员和相关企业并未退缩,积极探索并提出了一系列切实可行的解...
国产化替代产品目录   28  
热门文章
项目管理软件有哪些?
云禅道AD
禅道项目管理软件

云端的项目管理软件

尊享禅道项目软件收费版功能

无需维护,随时随地协同办公

内置subversion和git源码管理

每天备份,随时转为私有部署

免费试用