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

新手上路,请多包涵

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

 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" 之后的单词。

输出应如下所示:

 Subject: -Figures/Nautical and beach.

我试过下面的正则表达式:

 re.compile('(?<=subject)(.{30}(?:\s|.))',re.I)

但是主题关键字后没有固定的字数可以指定,所以我无法指定确切的字数。

如何在“peroid”或空格处停止。没有特定的停止标准。

原文由 niranjan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 483
1 个回答

您的 (?<=subject)(.{30}(?:\s|.)) 正则表达式断言 subject 之后的位置。然后抓取除换行符以外的 30 个字符,然后匹配空格或除换行符以外的任何字符。这并不真正符合您的要求,因为子字符串可以是任意长度。

您可以将基于交替的正则表达式与捕获组一起使用:

 subject:\s*([^.]+|\S+)

请参阅 正则表达式演示

详情

  • subject: - 文字 subject: 字符串
  • \s* - 0+ 空格
  • ([^.]+|\S+) - 第 1 组捕获 1 个或多个非句点符号或 1+ 个非空白符号

注意:备选方案的顺序在这里很 _重要_,因为 [^.]+ 匹配空格,而 \S+ 不匹配。如果 \s* 之后的子字符串以点开头,则 \S+ 将匹配该子字符串直至空格。

Python 演示

 import re
p = re.compile(r'subject:\s*([^.]+|\S+)', re.IGNORECASE)
s = "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"
m = p.search(s)
if m:
    print(m.group())    # this includes Subject:
    print(m.group(1))   # this does not include Subject:

原文由 Wiktor Stribiżew 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题