regex Python 正则 表达式 :当 文字 包含 特定 单字 时 , 比对 两 个 保留 字 之间 的 文字

e4eetjau  于 2022-11-18  发布在  Python
关注(0)|答案(2)|浏览(104)

我 正在 寻找 一 个 正则 表达式 来 匹配 包含 一 个 或 多 个 AND 的 OR 运算 符 之间 的 每个 句子 的 整个 文本 , 当 且 仅 当 一 个 或 多 个 AND 在 两 个 OR 之间 的 句子 中 。 例如 :
这 应该 与
x1月 1 日
OR "Message:\"Know the Ropes \<Meaning\>: Having a familiarity or understanding of how something works.\"" AND "Message:\"Poke Fun At \<Meaning\>: Making fun of something or someone; ridicule.\"" AND "Message:\"Give a Man a Fish \<Meaning\>: It's better to teach a person how to do something than to do that something for them.\"" AND "Message:\"Money Doesn't Grow On Trees \<Meaning\>: Suggests that money is a resource that must be earned and is not one that's easily acquired.\"" AND "Message:\"There's No I in Team \<Meaning\>: To not work alone, but rather, together with others in order to achieve a certain goal.\"" AND "Message:\"A Busy Bee \<Meaning\>: An industrious person.\"" AND "Message:\"Wake Up Call \<Meaning\>: An occurance of sorts that brings a problem to somebody's attention and they realize it needs fixing.\"" AND "Message:\"A Lot on One\'s Plate \<Meaning\>: A lot \(or too much\) to do or cope with.\"" AND "Message:\"Under the Weather \<Meaning\>: Not feeling well, in health or mood.\"" OR
这 不 应 匹配 :
OR "Message:\"Break The Ice \<Meaning\>: Breaking down a social stiffness.\"" OR
这 是 用作 示例 占位 符/随机 文本 :
"Message:\"Knock Your Socks Off \<Meaning\>: To be taken by surprise.\"" AND "Message:\"Playing For Keeps \<Meaning\>: Said when things are about to get serious.\"" OR "Message:\"Break The Ice \<Meaning\>: Breaking down a social stiffness.\"" OR "Message:\"Right Out of the Gate \<Meaning\>: Right from the beginning; to do something from the start.\"" OR "Message:\"Birds of a Feather Flock Together \<Meaning\>: People tend to associate with others who share similar interests or values.\"" AND "Message:\"Up In Arms \<Meaning\>: Angry; being roused to the point that you are ready to fight.\"" OR "Message:\"Know the Ropes \<Meaning\>: Having a familiarity or understanding of how something works.\"" AND "Message:\"Poke Fun At \<Meaning\>: Making fun of something or someone; ridicule.\"" AND "Message:\"Give a Man a Fish \<Meaning\>: It's better to teach a person how to do something than to do that something for them.\"" AND "Message:\"Money Doesn't Grow On Trees \<Meaning\>: Suggests that money is a resource that must be earned and is not one that's easily acquired.\"" AND "Message:\"There's No I in Team \<Meaning\>: To not work alone, but rather, together with others in order to achieve a certain goal.\"" AND "Message:\"A Busy Bee \<Meaning\>: An industrious person.\"" AND "Message:\"Wake Up Call \<Meaning\>: An occurance of sorts that brings a problem to somebody's attention and they realize it needs fixing.\"" AND "Message:\"A Lot on One\'s Plate \<Meaning\>: A lot \(or too much\) to do or cope with.\"" AND "Message:\"Under the Weather \<Meaning\>: Not feeling well, in health or mood.\"" OR "Message:\"A Day Late and a Dollar Short \<Meaning\>: Too late. A missed opportunity.\"" OR "Message:\"Back to Square One \<Meaning\>: To go back to the beginning; back to the drawing board.\"" OR "Message:\"An Arm and a Leg \<Meaning\>: Something that is extremely expensive.\"" AND "Message:\"Jaws of Death \<Meaning\>: Being in a dangerous or very deadly situation.\"" OR "Message:\"Barking Up The Wrong Tree \<Meaning\>: To make a wrong assumption about something.\"" OR "Message:\"Swinging For the Fences \<Meaning\>: Giving something your all.\"" OR "Message:\"Talk the Talk \<Meaning\>: Supporting what you say, not just with words, but also through action or evidence.\"" OR "Message:\"Back To the Drawing Board \<Meaning\>: Starting over again on a new design from a previously failed attempt.\"" OR "Message:\"On the Ropes \<Meaning\>: Being in a situation that looks to be hopeless!\"" OR "Message:\"Tug of War \<Meaning\>: It can refer to the popular rope pulling game or it can mean a struggle for authority.\"" AND "Message:\"A Dime a Dozen \<Meaning\>: Something that is extremely common.\"" AND "Message:\"In a Pickle \<Meaning\>: Being in a difficult predicament; a mess; an undesirable situation.\"" AND "Message:\"Ring Any Bells? \<Meaning\>: Recalling a memory; causing a person to remember something or someone.\"" AND "Message:\"When the Rubber Hits the Road \<Meaning\>: When something is about to begin, get serious, or put to the test.\"" AND "Message:\"Burst Your Bubble \<Meaning\>: To ruin someone's happy moment.\"" AND "Message:\"No Ifs, Ands, or Buts \<Meaning\>: Finishing a task without making any excuses.\"" AND "Message:\"Tough It Out \<Meaning\>: To remain resillient even in hard times; enduring.\"" OR "Message:\"Curiosity Killed The Cat \<Meaning\>: Typically said to indicate that any further investigation into a situation may lead to harm.\"" OR "Message:\"A Chip on Your Shoulder \<Meaning\>: Being angry about something that happened in the past.\"" OR "Message:\"A Cold Day in July \<Meaning\>: Something that is highly unlikely to happen.\"" OR "Message:\"Cry Over Spilt Milk \<Meaning\>: It's useless to worry about things that already happened and cannot be changed.\"" OR "Message:\"A Leg Up \<Meaning\>: Someone who's given an advantage over others.\"" OR "Message:\"It's Not Brain Surgery \<Meaning\>: A task that's easy to accomplish, a thing lacking complexity.\"" OR "Message:\"You Can't Judge a Book By Its Cover \<Meaning\>: Don't judge someone or something only by the outward appearance.\"" AND "Message:\"Down For The Count \<Meaning\>: Someone or something that looks to be defeated, or nearly so.\"" OR "Message:\"Yada Yada \<Meaning\>: A way to notify a person that what they're saying is predictable or boring.\"" AND "Message:\"Let Her Rip \<Meaning\>: Permission to start, or it could mean 'go faster!'\"" OR "Message:\"Wouldn't Harm a Fly \<Meaning\>: Nonviolent; someone who is mild or gentle.\"" OR "Message:\"Off One's Base \<Meaning\>: A person that is crazy or behaving in idiotic ways\"" AND "Message:\"Close But No Cigar \<Meaning\>: Coming close to a successful outcome only to fall short at the end.\"" AND "Message:\"It's Not All It's Cracked Up To Be \<Meaning\>: Failing to meet expectations; not being as good as people say.\"" AND "Message:\"What Am I, Chopped Liver? \<Meaning\>: A rhetorical question used by a person who feels they are being given less consideration than someone else.\"" AND "Message:\"A Dog in the Manger \<Meaning\>: Someone who prevents others from using valuable items even though they have no need for them.\"" AND "Message:\"A Bite at the Cherry \<Meaning\>: An opportunity that's not available to most people.\"" OR "Message:\"Don't Count Your Chickens Before They Hatch \<Meaning\>: Do not rely on something you are not sure of.\"
我 在 开始 时 使用 Positive lookbehind , 在 结束 时 使用 Positive lookahead 来 设置 边界 , 我 尝试 使用 ( . * ? AND . * ? ) 来 匹配 介于 零 和 无限 次 之间 的 任何 字符 , 并 尽 可能 地 减少 匹配 次数 。 我 尝试 使用 :
(?<=OR)(.*?AND.*?)(?=OR)
(?<=OR) (?:[\s\S])*? AND (?:[\s\S\w]+?)(?=OR)
它们 在 OR ( AND 之后 ) 处 停止 匹配 , 但 不会 在 AND 之前 的 第 一 个 OR 处 开始 匹配 。

yv5phkfx

yv5phkfx1#

如果我没有理解错的话,您希望在OR

(?<=OR)((?:(?!OR).)+AND(?:(?!OR).)+)(?=OR)

Regex demo.

ttp71kqs

ttp71kqs2#

我建议使用一个简单的正则表达式来按' OR '进行拆分,并在列表解析中调整结果,而不是使用一个复杂的正则表达式进行搜索(为了加快速度并提高代码的可读性):

message = '''"Message:\"Knock Your Socks Off \<Meaning\>: To be taken by surprise.\"" AND "Message:\"Playing For Keeps \<Meaning\>: Said when things are about to get serious.\"" OR  "Message:\"Break The Ice \<Meaning\>: Breaking down a social stiffness.\"" OR "Message:\"Right Out of the Gate \<Meaning\>: Right from the beginning; to do something from the start.\"" OR "Message:\"Birds of a Feather Flock Together \<Meaning\>: People tend to associate with others who share similar interests or values.\"" AND "Message:\"Up In Arms \<Meaning\>: Angry; being roused to the point that you are ready to fight.\"" OR "Message:\"Know the Ropes \<Meaning\>: Having a familiarity or understanding of how something works.\"" AND "Message:\"Poke Fun At \<Meaning\>: Making fun of something or someone; ridicule.\"" AND "Message:\"Give a Man a Fish \<Meaning\>: It's better to teach a person how to do something than to do that something for them.\"" AND "Message:\"Money Doesn't Grow On Trees \<Meaning\>: Suggests that money is a resource that must be earned and is not one that's easily acquired.\"" AND "Message:\"There's No I in Team \<Meaning\>: To not work alone, but rather, together with others in order to achieve a certain goal.\"" AND "Message:\"A Busy Bee \<Meaning\>: An industrious person.\"" AND "Message:\"Wake Up Call \<Meaning\>: An occurance of sorts that brings a problem to somebody's attention and they realize it needs fixing.\"" AND "Message:\"A Lot on One\'s Plate \<Meaning\>: A lot \(or too much\) to do or cope with.\"" AND "Message:\"Under the Weather \<Meaning\>: Not feeling well, in health or mood.\"" OR "Message:\"A Day Late and a Dollar Short \<Meaning\>: Too late. A missed opportunity.\"" OR "Message:\"Back to Square One \<Meaning\>: To go back to the beginning; back to the drawing board.\"" OR "Message:\"An Arm and a Leg \<Meaning\>: Something that is extremely expensive.\"" AND "Message:\"Jaws of Death \<Meaning\>: Being in a dangerous or very deadly situation.\"" OR "Message:\"Barking Up The Wrong Tree \<Meaning\>: To make a wrong assumption about something.\"" OR "Message:\"Swinging For the Fences \<Meaning\>: Giving something your all.\"" OR "Message:\"Talk the Talk \<Meaning\>: Supporting what you say, not just with words, but also through action or evidence.\"" OR "Message:\"Back To the Drawing Board \<Meaning\>: Starting over again on a new design from a previously failed attempt.\"" OR "Message:\"On the Ropes \<Meaning\>: Being in a situation that looks to be hopeless!\"" OR "Message:\"Tug of War \<Meaning\>: It can refer to the popular rope pulling game or it can mean a struggle for authority.\"" AND "Message:\"A Dime a Dozen \<Meaning\>: Something that is extremely common.\"" AND "Message:\"In a Pickle \<Meaning\>: Being in a difficult predicament; a mess; an undesirable situation.\"" AND "Message:\"Ring Any Bells? \<Meaning\>: Recalling a memory; causing a person to remember something or someone.\"" AND "Message:\"When the Rubber Hits the Road \<Meaning\>: When something is about to begin, get serious, or put to the test.\"" AND "Message:\"Burst Your Bubble \<Meaning\>: To ruin someone's happy moment.\"" AND "Message:\"No Ifs, Ands, or Buts \<Meaning\>: Finishing a task without making any excuses.\"" AND "Message:\"Tough It Out \<Meaning\>: To remain resillient even in hard times; enduring.\"" OR "Message:\"Curiosity Killed The Cat \<Meaning\>: Typically said to indicate that any further investigation into a situation may lead to harm.\"" OR "Message:\"A Chip on Your Shoulder \<Meaning\>: Being angry about something that happened in the past.\"" OR "Message:\"A Cold Day in July \<Meaning\>: Something that is highly unlikely to happen.\"" OR "Message:\"Cry Over Spilt Milk \<Meaning\>: It's useless to worry about things that  already happened and cannot be changed.\"" OR "Message:\"A Leg Up \<Meaning\>: Someone who's given an advantage over others.\"" OR "Message:\"It's Not Brain Surgery \<Meaning\>: A task that's easy to accomplish, a thing lacking complexity.\"" OR "Message:\"You Can't Judge a Book By Its Cover \<Meaning\>: Don't judge someone or something only by the outward appearance.\"" AND "Message:\"Down For The Count \<Meaning\>: Someone or something that looks to be defeated, or nearly so.\"" OR "Message:\"Yada Yada \<Meaning\>: A way to notify a person that what they're saying is predictable or boring.\"" AND "Message:\"Let Her Rip \<Meaning\>: Permission to start, or it could mean 'go faster!'\"" OR "Message:\"Wouldn't Harm a Fly \<Meaning\>: Nonviolent; someone who is mild or gentle.\"" OR "Message:\"Off One's Base \<Meaning\>: A person that is crazy or behaving in idiotic ways\"" AND "Message:\"Close But No Cigar \<Meaning\>: Coming close to a successful outcome only to fall short at the end.\"" AND "Message:\"It's Not All It's Cracked Up To Be \<Meaning\>: Failing to meet expectations; not being as good as people say.\"" AND "Message:\"What Am I, Chopped Liver? \<Meaning\>: A rhetorical question used by a person who feels they are being given less consideration than someone else.\"" AND "Message:\"A Dog in the Manger \<Meaning\>: Someone who prevents others from using valuable items even though they have no need for them.\"" AND "Message:\"A Bite at the Cherry \<Meaning\>: An opportunity that's not available to most people.\"" OR "Message:\"Don't Count Your Chickens Before They Hatch \<Meaning\>: Do not rely on something you are not sure of.\"'''
import re
selection = [ item for item in re.split(' OR ', message)[1:-1] 
                  if ' AND ' in item ]
print(*selection, sep='\n')

给予

"Message:"Birds of a Feather Flock Together \<Meaning\>: People tend to associate with others who share similar interests or values."" AND "Message:"Up In Arms \<Meaning\>: Angry; being roused to the point that you are ready to fight.""
"Message:"Know the Ropes \<Meaning\>: Having a familiarity or understanding of how something works."" AND "Message:"Poke Fun At \<Meaning\>: Making fun of something or someone; ridicule."" AND "Message:"Give a Man a Fish \<Meaning\>: It's better to teach a person how to do something than to do that something for them."" AND "Message:"Money Doesn't Grow On Trees \<Meaning\>: Suggests that money is a resource that must be earned and is not one that's easily acquired."" AND "Message:"There's No I in Team \<Meaning\>: To not work alone, but rather, together with others in order to achieve a certain goal."" AND "Message:"A Busy Bee \<Meaning\>: An industrious person."" AND "Message:"Wake Up Call \<Meaning\>: An occurance of sorts that brings a problem to somebody's attention and they realize it needs fixing."" AND "Message:"A Lot on One's Plate \<Meaning\>: A lot \(or too much\) to do or cope with."" AND "Message:"Under the Weather \<Meaning\>: Not feeling well, in health or mood.""
"Message:"An Arm and a Leg \<Meaning\>: Something that is extremely expensive."" AND "Message:"Jaws of Death \<Meaning\>: Being in a dangerous or very deadly situation.""
"Message:"Tug of War \<Meaning\>: It can refer to the popular rope pulling game or it can mean a struggle for authority."" AND "Message:"A Dime a Dozen \<Meaning\>: Something that is extremely common."" AND "Message:"In a Pickle \<Meaning\>: Being in a difficult predicament; a mess; an undesirable situation."" AND "Message:"Ring Any Bells? \<Meaning\>: Recalling a memory; causing a person to remember something or someone."" AND "Message:"When the Rubber Hits the Road \<Meaning\>: When something is about to begin, get serious, or put to the test."" AND "Message:"Burst Your Bubble \<Meaning\>: To ruin someone's happy moment."" AND "Message:"No Ifs, Ands, or Buts \<Meaning\>: Finishing a task without making any excuses."" AND "Message:"Tough It Out \<Meaning\>: To remain resillient even in hard times; enduring.""
"Message:"You Can't Judge a Book By Its Cover \<Meaning\>: Don't judge someone or something only by the outward appearance."" AND "Message:"Down For The Count \<Meaning\>: Someone or something that looks to be defeated, or nearly so.""
"Message:"Yada Yada \<Meaning\>: A way to notify a person that what they're saying is predictable or boring."" AND "Message:"Let Her Rip \<Meaning\>: Permission to start, or it could mean 'go faster!'""
"Message:"Off One's Base \<Meaning\>: A person that is crazy or behaving in idiotic ways"" AND "Message:"Close But No Cigar \<Meaning\>: Coming close to a successful outcome only to fall short at the end."" AND "Message:"It's Not All It's Cracked Up To Be \<Meaning\>: Failing to meet expectations; not being as good as people say."" AND "Message:"What Am I, Chopped Liver? \<Meaning\>: A rhetorical question used by a person who feels they are being given less consideration than someone else."" AND "Message:"A Dog in the Manger \<Meaning\>: Someone who prevents others from using valuable items even though they have no need for them."" AND "Message:"A Bite at the Cherry \<Meaning\>: An opportunity that's not available to most people.""

请注意,这种方法(以及在其他答案中搜索的方法)不包括消息文本中出现OR和AND的情况,因为没有检查确保在引号之外找到OR和AND。
现在,我们来比较一下使用拆分的方法和使用搜索的方法:

import re
from time import perf_counter as T
sT_1=T()
selection_1 = [item for item in re.split(' OR ', message)[1:-1] if ' AND ' in item]
eT_1=T()
sT_2=T()
selection_2 = re.findall('(?<= OR )((?:(?! OR ).)+ AND (?:(?! OR ).)+)(?= OR )',message)
eT_2=T()
assert selection_1 == selection_2
print(f'{(eT_2-sT_2) - (eT_1-sT_1):8.6f}, {(eT_1-sT_1):8.6f}, {(eT_2-sT_2):8.6f}')

它打印:

0.000300, 0.000150, 0.000450

示出了使用分裂的方法比使用搜索的方法运行快3倍。

相关问题