正则表达式在python中找到一个字符串

时间:2018-01-18 16:37:45

标签: python regex

输入

text = "Store Item Number (DPCI)=244-02-6685;Pop Musical Style=Arena Rock, Album Rock, Pop-Metal, Hard Rock, Hair Metal, Heavy Metal;Origin=Made in the USA or Imported;Record label=UNIVERSAL INT'L;...."

输出

(Store Item Number (DPCI),Pop Musical Style, Origin, Record label) 

我需要一个正则表达式

3 个答案:

答案 0 :(得分:0)

\w+会匹配任何字词,但您正在尝试匹配特定的字符串。

(Store Item Number \(DPCI\)|Pop Musical Style|Origin|Record label)\=

答案 1 :(得分:0)

您可以使用lookbehinds在Store Item Number (DPCI)Pop Musical Style后抓取数据:

import re
text = "Store Item Number (DPCI)=244-02-6685;Pop Musical Style=Arena Rock, Album Rock, Pop-Metal, Hard Rock, Hair Metal, Heavy Metal;Origin=Made in the USA or Imported;Record label=UNIVERSAL INT'L;"
data = re.findall('(?<=Store Item Number \(DPCI\)\=)[\d\-]+|(?<=Pop Musical Style\=)[a-zA-Z\s,]+', text)

输出:

['244-02-6685', 'Arena Rock, Album Rock, Pop']

答案 2 :(得分:0)

请参阅here示例:

^[^=]+|(?<=;)[^=]+

;=之间找到元素,或者从输入的开头开始并以=结尾的元素。

输出:['Store Item Number (DPCI)', 'Pop Musical Style', 'Origin', 'Record label']