Python 正则表达式查找特定单词后的所有单词?

Python 正则表达式查找特定单词后的所有单词?,python,regex,python-3.x,pattern-matching,Python,Regex,Python 3.x,Pattern Matching,我有一个字符串,如下所示: Features: -Includes hanging accessories. -Artist: William-Adolphe Bouguereau. -Made with 100pct cotton canvas. -100pct Anti-shrink pine wood bars and Epson anti-fade ultra chrome inks. -100pct Hand-made and inspected in the U.S.A.

我有一个字符串,如下所示:

Features:  -Includes hanging accessories.  -Artist: William-Adolphe Bouguereau.  -Made with 100pct cotton canvas.  -100pct Anti-shrink pine wood bars and Epson anti-fade ultra chrome inks.  -100pct Hand-made and inspected in the U.S.A.  -Orientation: Horizontal.  **Subject: -Figures/Nautical and beach.**  Gender: -Unisex/Both.  Size: -Mini 17'' and under/Small 18''-24''/Medium 25''-32''/Large 33''-40''/Oversized 41'' and above.  Style: -Fine art.  Color: -Blue.  Country of Manufacture: -United States.  Product Type: -Print of painting.  Region: -Europe.  Primary Art Material: -Canvas. Dimensions:  -8'' H x 12'' W x 0.75'' D: 0.72 lb.  -12'' H x 18'' W x 0.75'' D: 1.14 lbs.  -12'' H x 18'' W x 1.5'' D: 2.45 lbs.  -18'' H x 26'' W x 0.75'' D: 1.44 lbs.  Paintings Prints Tori White Wildon Photography Photos Posters Abstract Black D cor Designs Framed Hazelwood Hokku Home Landscape Oil Accent 075 12 15 18 26 40 60 8 D H W x 1 1017 1824 2532 holidays, christmas gift gifts for girls boys
Subject: -Figures/Nautical and beach.
我必须在特定的单词后面找到单词

我想提取上面示例中单词
“Subject”
后面的单词

输出应如下所示:

Features:  -Includes hanging accessories.  -Artist: William-Adolphe Bouguereau.  -Made with 100pct cotton canvas.  -100pct Anti-shrink pine wood bars and Epson anti-fade ultra chrome inks.  -100pct Hand-made and inspected in the U.S.A.  -Orientation: Horizontal.  **Subject: -Figures/Nautical and beach.**  Gender: -Unisex/Both.  Size: -Mini 17'' and under/Small 18''-24''/Medium 25''-32''/Large 33''-40''/Oversized 41'' and above.  Style: -Fine art.  Color: -Blue.  Country of Manufacture: -United States.  Product Type: -Print of painting.  Region: -Europe.  Primary Art Material: -Canvas. Dimensions:  -8'' H x 12'' W x 0.75'' D: 0.72 lb.  -12'' H x 18'' W x 0.75'' D: 1.14 lbs.  -12'' H x 18'' W x 1.5'' D: 2.45 lbs.  -18'' H x 26'' W x 0.75'' D: 1.44 lbs.  Paintings Prints Tori White Wildon Photography Photos Posters Abstract Black D cor Designs Framed Hazelwood Hokku Home Landscape Oil Accent 075 12 15 18 26 40 60 8 D H W x 1 1017 1824 2532 holidays, christmas gift gifts for girls boys
Subject: -Figures/Nautical and beach.
我尝试了以下正则表达式:

re.compile('(?<=subject)(.{30}(?:\s|.))',re.I)
重新编译(')(?你的
(?试试:


正则表达式:

(Subject:.+)\*\*

Match Subject and content after that till '**'
str = 'Features:  -Includes hanging accessories.  -Artist: William-Adolphe Bouguereau.  -Made with 100pct cotton canvas.  -100pct Anti-shrink pine wood bars and Epson anti-fade ultra chrome inks.  -100pct Hand-made and inspected in the U.S.A.  -Orientation: Horizontal.  **Subject: -Figures/Nautical and beach.**  Gender: -Unisex/Both.  Size: -Mini 17'' and under/Small 18''-24''/Medium 25''-32''/Large 33''-40''/Oversized 41'' and above.  Style: -Fine art.  Color: -Blue.  Country of Manufacture: -United States.  Product Type: -Print of painting.  Region: -Europe.  Primary Art Material: -Canvas. Dimensions:  -8'' H x 12'' W x 0.75'' D: 0.72 lb.  -12'' H x 18'' W x 0.75'' D: 1.14 lbs.  -12'' H x 18'' W x 1.5'' D: 2.45 lbs.  -18'' H x 26'' W x 0.75'' D: 1.44 lbs.  Paintings Prints Tori White Wildon Photography Photos Posters Abstract Black D cor Designs Framed Hazelwood Hokku Home Landscape Oil Accent 075 12 15 18 26 40 60 8 D H W x 1 1017 1824 2532 holidays, christmas gift gifts for girls boys'
import re

a = re.search(r'(Subject:.+)\*\*',str)
print(a.group(1))
代码:

(Subject:.+)\*\*

Match Subject and content after that till '**'
str = 'Features:  -Includes hanging accessories.  -Artist: William-Adolphe Bouguereau.  -Made with 100pct cotton canvas.  -100pct Anti-shrink pine wood bars and Epson anti-fade ultra chrome inks.  -100pct Hand-made and inspected in the U.S.A.  -Orientation: Horizontal.  **Subject: -Figures/Nautical and beach.**  Gender: -Unisex/Both.  Size: -Mini 17'' and under/Small 18''-24''/Medium 25''-32''/Large 33''-40''/Oversized 41'' and above.  Style: -Fine art.  Color: -Blue.  Country of Manufacture: -United States.  Product Type: -Print of painting.  Region: -Europe.  Primary Art Material: -Canvas. Dimensions:  -8'' H x 12'' W x 0.75'' D: 0.72 lb.  -12'' H x 18'' W x 0.75'' D: 1.14 lbs.  -12'' H x 18'' W x 1.5'' D: 2.45 lbs.  -18'' H x 26'' W x 0.75'' D: 1.44 lbs.  Paintings Prints Tori White Wildon Photography Photos Posters Abstract Black D cor Designs Framed Hazelwood Hokku Home Landscape Oil Accent 075 12 15 18 26 40 60 8 D H W x 1 1017 1824 2532 holidays, christmas gift gifts for girls boys'
import re

a = re.search(r'(Subject:.+)\*\*',str)
print(a.group(1))

你有什么理由不能在
**
上进行拆分并为结果列表编制索引吗?
s.split(“**”[1]
产生的正是你想要的结果。如果你的问题没有很好的定义,答案也会很容易找到。“period或space”的正则表达式是
[\s.]
但这显然不是你问题的解决方案。仔细想想你实际需要什么。很多答案都是关于星号的,很明显,你放星号只是为了突出你想要提取的字段。也许你应该把它们拿出来,这样你的示例输入就正好是你实际需要的示例需要处理。我认为任何人都不会误解“提取
主题:
值”这句话(尽管不清楚如何确定它的结尾)。它正确地给出了输出。
[^.]
匹配任何字符(包括空格),我想知道它不会在“空白”处停止,因为有时没有句号或任何其他非单词字符。我现在还添加了一个注释,解释为什么需要
\S+
。该
[^.]
匹配任何字符(包括空格)和<代码> +>代码>使其匹配一次或多次GRIDELI.考虑此字符串“运动和运动队
  • 性别:中性
  • 风格:当代
  • 媒体:Giclee印刷
  • 产品类型:平面艺术
  • 主要艺术材料:帆布

  • 尺寸
    36英寸高x 24英寸宽x 1.5英寸在您在评论中提供的字符串中没有“主题”。对不起。"主题:运动和运动队性别:中性风格:当代风格:Giclee印刷品类型:平面艺术主要艺术材料:画布尺寸:36英寸高x 24英寸宽x 1.5英寸注:您不需要在字符类内转义
    *
    ,如
    *
    内部的
    […]
    失去了其量词的原始含义。
    {2}
    使
    [^*{2}]+
    模式的意思是:除
    *
    {
    2
    ]之外的一个或多个字符
    。事实上,输入中似乎缺少期望值后的星号。我从OP注释到我的答案中推断出它。事实上,输入中似乎缺少期望值后的星号。我从OP注释到我的答案中推断出它。