2017-02-22 25 views
0

我似乎無法弄清楚如何做到這一點。任何幫助將不勝感激。正則表達式過濾產品線和型號(Python)

基本上我試圖從不同的字符串中篩選出產品系列和產品編號。 (假設所有的產品線會之前的產品型號和模型將始終包含數)

Latitude E6430 (Latitude E6430) 
HP EliteBook 8460p (H3S08US#ABA) 
ThinkPad T60 

我預期的結果:

Product line: Latitude 

Model: E6430 

Product line: EliteBook 

Model: 8460p 

Product line: ThinkPad 
Model: T60 

在先進的感謝

+0

任何嘗試嗎? – Rahul

+0

爲什麼要使用正則表達式? –

+0

**模型將始終包含數字**在給定示例中自相矛盾(E6430,8460p) – ZdaR

回答

0

我會去對於以下正則表達式與re.search

s= 'Latitude E6430 (Latitude E6430)' 
m = re.search('([a-zA-Z]+) ([a-zA-Z]*\d+[a-zA-Z]*)', s) 

然後

m.group(1) # Latitude 
m.group(2) # E6430 

正則表達式的解釋是

NODE      EXPLANATION 
-------------------------------------------------------------------------------- 
    (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    [a-zA-Z]+    any character of: 'a' to 'z', 'A' to 'Z' 
          (1 or more times (matching the most 
          amount possible)) 
-------------------------------------------------------------------------------- 
)      end of \1 
-------------------------------------------------------------------------------- 
          ' ' 
-------------------------------------------------------------------------------- 
    (      group and capture to \2: 
-------------------------------------------------------------------------------- 
    [a-zA-Z]*    any character of: 'a' to 'z', 'A' to 'Z' 
          (0 or more times (matching the most 
          amount possible)) 
-------------------------------------------------------------------------------- 
    \d+      digits (0-9) (1 or more times (matching 
          the most amount possible)) 
-------------------------------------------------------------------------------- 
    [a-zA-Z]*    any character of: 'a' to 'z', 'A' to 'Z' 
          (0 or more times (matching the most 
          amount possible)) 
-------------------------------------------------------------------------------- 
)      end of \2 
+0

我想出了類似的東西。但是這也會在'(​​)'內搜索,結果是重複的結果。 [演示](https://regex101.com/r/MKtSE5/1) – Rahul

+0

@Rahul這就是爲什麼我使用re.search並捕獲第一和第二組以避免其餘匹配。 –

+0

嗷!蟒蛇。對 !!我的錯。對不起! – Rahul