2012-03-06 44 views
1

說我有以下文字:建立一個正則表達式匹配前多次出現在Python

*<string1>*<string2>*<string3>* 

其中*表示,除了事物所包圍<>任何文本。

我希望能夠捕獲string1(文字第一次出現的字符<>)。

我已經嘗試使用:

r = re.compile('.*<(.*?)>.*(<.*?>)*.*') 
r.search(my_text) 
match = m.group(1) 

,但沒有奏效。

我沒有問題捕捉string1用一個簡單的正則表達式,如果文本只有一個出現的<>與包圍的字符串:

r = re.compile('.*<.*?>.*') 

但我不能確定正確的正則表達式當文本有多個常用表達。我不確定我是否正確理解()?對此問題的作用。

如何捕獲上面頂部示例中的第一個string1

+0

它看起來像你試圖用正則表達式解析html/xml。需要幫助嗎? :) – 2012-03-06 01:24:27

+0

@gnibbler。這不是'html'和'xml'。它是由[Platform LSF](http://en.wikipedia.org/wiki/Platform_LSF)中的shell命令'bjobs'產生的輸出 – 2012-03-06 01:25:25

+0

你看過[finditer](http://docs.python.org /library/re.html#re.finditer)? – 2012-03-06 01:27:43

回答

0

這應該這樣做

re.search("<([^>]*)", the_string).group(1) 
1

試試這個正則表達式:

import re 

my_text = "*<string1>*<string2>*<string3>*" 
r = re.compile('(?<=\<)[^>]*') 

print r.search(my_text).group(0) 

print r.findall(my_text) #This will get an array of all matches. 

(?<=\<)是回顧後,這意味着支票匹配,但不捕獲

相關問題